Java IO体系之OutputStreamWriter

Java IO体系之OutputStreamWriter介绍字符输出流Writer的实现类继承关系图源码packagejava.io;importjava.nio.charset.Charset;importjava.nio.charset.CharsetEncoder;importsun.nio.cs.StreamEncoder;publicclassOutputStreamWriterextendsWri…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

介绍

  • 字符输出流Writer 的实现类

继承关系图

在这里插入图片描述

样例

public static void main(String[] args) throws IOException{ 
   
    //创建字节输出流,绑定数据文件(没有则创建)
    FileOutputStream fos=new FileOutputStream("D:\\HELLO.txt");
    // 创建转换流对象,构造方法,绑定字节输出流
    OutputStreamWriter osw=new OutputStreamWriter(fos, "UTF-8");
    //转换流写数据(覆盖写)
    osw.append("春天到了").append("\r\n").append("春暖花开");
    osw.close();
    fos.close();
}

源码

package java.io;

import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import sun.nio.cs.StreamEncoder;

public class OutputStreamWriter extends Writer { 
   

    private final StreamEncoder se;

    /** * Creates an OutputStreamWriter that uses the named charset. */
    public OutputStreamWriter(OutputStream out, String charsetName)
        throws UnsupportedEncodingException
    { 
   
        super(out);
        if (charsetName == null)
            throw new NullPointerException("charsetName");
        se = StreamEncoder.forOutputStreamWriter(out, this, charsetName);
    }

    /** * Creates an OutputStreamWriter that uses the default character encoding. * * @param out An OutputStream */
    public OutputStreamWriter(OutputStream out) { 
   
        super(out);
        try { 
   
            se = StreamEncoder.forOutputStreamWriter(out, this, (String)null);
        } catch (UnsupportedEncodingException e) { 
   
            throw new Error(e);
        }
    }

    /** * Creates an OutputStreamWriter that uses the given charset. * * @since 1.4 * @spec JSR-51 */
    public OutputStreamWriter(OutputStream out, Charset cs) { 
   
        super(out);
        if (cs == null)
            throw new NullPointerException("charset");
        se = StreamEncoder.forOutputStreamWriter(out, this, cs);
    }

    /** * Creates an OutputStreamWriter that uses the given charset encoder. * * @since 1.4 * @spec JSR-51 */
    public OutputStreamWriter(OutputStream out, CharsetEncoder enc) { 
   
        super(out);
        if (enc == null)
            throw new NullPointerException("charset encoder");
        se = StreamEncoder.forOutputStreamWriter(out, this, enc);
    }

    /** * Returns the name of the character encoding being used by this stream. * * <p> If the encoding has an historical name then that name is returned; * otherwise the encoding's canonical name is returned. * * * @revised 1.4 * @spec JSR-51 */
    public String getEncoding() { 
   
        return se.getEncoding();
    }

    /** * Flushes the output buffer to the underlying byte stream, without flushing * the byte stream itself. This method is non-private only so that it may * be invoked by PrintStream. */
    void flushBuffer() throws IOException { 
   
        se.flushBuffer();
    }

    /** * Writes a single character. * * @exception IOException If an I/O error occurs */
    public void write(int c) throws IOException { 
   
        se.write(c);
    }

    /** * Writes a portion of an array of characters. * * @param cbuf Buffer of characters * @param off Offset from which to start writing characters * @param len Number of characters to write * * @exception IOException If an I/O error occurs */
    public void write(char cbuf[], int off, int len) throws IOException { 
   
        se.write(cbuf, off, len);
    }

    /** * Writes a portion of a string. * * @param str A String * @param off Offset from which to start writing characters * @param len Number of characters to write * * @exception IOException If an I/O error occurs */
    public void write(String str, int off, int len) throws IOException { 
   
        se.write(str, off, len);
    }

    /** * Flushes the stream. * * @exception IOException If an I/O error occurs */
    public void flush() throws IOException { 
   
        se.flush();
    }

    public void close() throws IOException { 
   
        se.close();
    }
}

FileWriter源码

package java.io;

public class FileWriter extends OutputStreamWriter { 
   

    /** * Constructs a FileWriter object given a file name. * * @param fileName String The system-dependent filename. * @throws IOException if the named file exists but is a directory rather * than a regular file, does not exist but cannot be * created, or cannot be opened for any other reason */
    public FileWriter(String fileName) throws IOException { 
   
        super(new FileOutputStream(fileName));
    }

    /** * Constructs a FileWriter object given a file name with a boolean * indicating whether or not to append the data written. * * @param fileName String The system-dependent filename. * @param append boolean if <code>true</code>, then data will be written * to the end of the file rather than the beginning. * @throws IOException if the named file exists but is a directory rather * than a regular file, does not exist but cannot be * created, or cannot be opened for any other reason */
    public FileWriter(String fileName, boolean append) throws IOException { 
   
        super(new FileOutputStream(fileName, append));
    }

    /** * Constructs a FileWriter object given a File object. * * @param file a File object to write to. * @throws IOException if the file exists but is a directory rather than * a regular file, does not exist but cannot be created, * or cannot be opened for any other reason */
    public FileWriter(File file) throws IOException { 
   
        super(new FileOutputStream(file));
    }

    /** * Constructs a FileWriter object given a File object. If the second * argument is <code>true</code>, then bytes will be written to the end * of the file rather than the beginning. * * @param file a File object to write to * @param append if <code>true</code>, then bytes will be written * to the end of the file rather than the beginning * @throws IOException if the file exists but is a directory rather than * a regular file, does not exist but cannot be created, * or cannot be opened for any other reason * @since 1.4 */
    public FileWriter(File file, boolean append) throws IOException { 
   
        super(new FileOutputStream(file, append));
    }

    /** * Constructs a FileWriter object associated with a file descriptor. * * @param fd FileDescriptor object to write to. */
    public FileWriter(FileDescriptor fd) { 
   
        super(new FileOutputStream(fd));
    }

}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/194792.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • MySQL连接查询

    MySQL连接查询首先创建两个表fruits表,包含水果id、名字、价格orders表,包含id和订单号(num)1.内连接查询(INNORJOIN)使用普通sql语句selectfruits.id,name,price,numfromfruits,orderswherefruits.id=orders.id;使用内连接查询语句(结果与上图相同)s…

  • kettle工具 学习

    kettle工具 学习背景:项目需要从客户的数据库迁移一批人口数据,大约240w条。所以我们需要一款工具帮助我们实现数据快速搬运,数据过滤,以得到符合我们使用的安全数据。

    2022年10月10日
  • vue封装组件思路_前端封装组件

    vue封装组件思路_前端封装组件父组件引用子组件,设置props<addtableName=”mysql”/>vue子组件初始化created(){//在组件初始化的时候执行,只执行一次console.log(this.$data);console.log(this);}vue中子组件的method…

  • 深入理解okio的优化思想

    深入理解okio的优化思想随着越来越多的应用使用OKHttp来进行网络访问,我们有必要去深入研究OKHTTP的基石,一套更加轻巧方便高效的IO库okio.OKIO的优点有同学或会问,目前Java的IO已经非常成熟了,为什么还要使用新的IO库呢?笔者认为,答案有以下几点:低的CPU和内存消耗。后面我们会分析到,okio采用了segment的机制进行内存共享和复用,尽可能少的去申请内存,同时也就降低了GC的频率。我们知道,过于

  • javascript 换行符[通俗易懂]

    javascript 换行符[通俗易懂] 在JS的字符串里对[TAB]的表述是 /x09  你可以做这样的测试: alert(“/x41”); //看看得到是什么?? 是字母A(41是十六进制的ASCII码值)  我喜欢用 /x0f 这类的做分隔符, [TAB]键用户还是有可能输入的, 但 /x0f 就绝对不可能输入  var s = “A/x0fB/x0fC/x0f

  • Java反射介绍[通俗易懂]

    Java反射介绍[通俗易懂] 一、反射的概述JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。要想解剖一个类,必须先要获取到该类的字节码文件对象。而解剖使用的就是Class类中的方法.所以先要获取到每一个字节码文件对应的Class类型的对象.以上的总结就…

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号