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));
    }

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

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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • [分享] 大环境下瑟瑟发抖辞职的第二天,拿了两个 offer[通俗易懂]

    [分享] 大环境下瑟瑟发抖辞职的第二天,拿了两个 offer

    2022年2月14日
    53
  • SecureCRT的使用教程( 过程非常详细!!值得查看)

    SecureCRT的使用教程( 过程非常详细!!值得查看)SecureCRT的使用教程(过程非常详细!!值得查看)下载和安装简单的介绍一、SecureCRT的使用二、SecureFX的使用下载和安装SecureCRT和SecureFX的下载和安装我这里就不多说了,详细的安装下载教程请参考:SecureCRT的下载、安装(过程非常详细!!值得查看)简单的介绍SecureCRT是一款支持SSH(SSH1和SSH2)的终端仿真程序,简单地说是…

    2022年4月29日
    286
  • 字符数组反转_字符数组的初始化

    字符数组反转_字符数组的初始化关于字符串的反转,倒是很简单,但是编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组char[]的形式给出。不要给另外的数组分配额外的空间,你必须原地修改输入数组、使用O(1)的额外空间解决这一问题。你可以假设数组中的所有字符都是ASCII码表中的可打印字符。示例1:输入:[“h”,“e”,“l”,“l”,“o”]输出:[“o”,“l”,“l”,“e”,“h…

    2022年5月3日
    48
  • 快速排序基本思路(通俗易懂+例子)「建议收藏」

    快速排序基本思路(通俗易懂+例子)「建议收藏」快速排序今天看到大神写的一篇快速排序的博客,肃然起敬,觉得原来快速排序这么简单下面进行简单的试试快速排序的基本思想是1、先从数列中取出一个数作为基准数2、分区过程,将比这个数大的数全放到它的右边,小于或等于它的数全放到它的左边3、再对左右区间重复第二步,直到各区间只有一个数概括来说为挖坑填数+分治法下面举例来进行说明,主要有三个参数,i为区间的开始地址,j为区间

    2022年6月15日
    25
  • pytorch学习笔记七:torchvision.datasets.ImageFolder使用详解

    pytorch学习笔记七:torchvision.datasets.ImageFolder使用详解一、数据集组织方式ImageFolder是一个通用的数据加载器,它要求我们以下面这种格式来组织数据集的训练、验证或者测试图片。root/dog/xxx.pngroot/dog/xxy.pngroot/dog/xxz.pngroot/cat/123.pngroot/cat/nsdf3.pngroot/cat/asd932_.png对于上面的root,假设data文件夹在.py文件…

    2022年6月24日
    35
  • python pandas fillna_pandas删除行

    python pandas fillna_pandas删除行DataFrame.fillna(self,value=None,method=None,axis=None,inplace=False,limit=None,downcast=None,**kwargs)使用指定的方法填充NA/NaN值。参数:value:scalar(标量),dict,Series,或DataFrame用于填充孔的值(例如0),或者是dict/Seri…

    2022年8月12日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

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