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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Pycharm中设置默认字符编码为 utf-8模版

    Pycharm中设置默认字符编码为 utf-8模版呃…又来水一篇供上廖雪峰的python教程中关于string和encoding的讲解在计算机内存中,统一使用Unicode编码,当需要保存到硬盘或者需要传输的时候,就转换为UTF-8编码。用记事本编辑的时候,从文件读取的UTF-8字符被转换为Unicode字符到内存里,编辑完成后,保存的时候再把Unicode转换为UTF-8保存到文件;浏览网页的时候,服务器会把动态生成的Unico…

    2022年8月26日
    15
  • PyCharm professionl2021激活码【在线破解激活】

    PyCharm professionl2021激活码【在线破解激活】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月16日
    61
  • 好东西!

    好东西!

    2021年7月28日
    59
  • 模糊控制器matlab仿真_有关bp神经网络Matlab的书

    模糊控制器matlab仿真_有关bp神经网络Matlab的书以下多套系统源码:1、MATLAB二级倒立摆三级倒立摆(在MATLAB下的二级倒立摆、三级倒立摆的仿真。内有所有需要的m文件)2、倒立摆matlab代码3、神经网络倒立摆控制(利用matlab对倒立摆的仿真,效果十分不错)4、二级倒立摆模型(这是一个演示直线二级倒立摆的matlab运行模型,这个模型能够根据仿真数据动画演示出二级倒立摆的运动情况。)5、倒立摆源码(倒立摆源码程序,注释很详细,是学习倒立摆原理,PID算法很好的参考资料。代码书写规范,注释详细。)6、二级倒立摆神经网络控制7

    2022年8月18日
    6
  • 数据库课程设计

    图书管理系统1.概述项目背景2.需求分析2.1系统需求2.2数据需求2.3数据字典2.3.1书籍信息表2.3.2库存信息表2.3.4顾客信息表2.3.5管理员信息表2.3.6图书类型信息表2.3.7订单详细信息表3.数据库设计3.1…

    2022年4月3日
    40
  • eclipse全文搜索快捷键_eclipse查看被引用快捷键

    eclipse全文搜索快捷键_eclipse查看被引用快捷键文件太多单个找太麻烦,eclipse提供了全局搜索的功能,下面就和大家分享一下如何使用eclipse全局搜索功能。eclipse全局搜索快捷键是什么全局搜索快捷键是Ctrl+H。搜索功能具体用法如下:1、打开eclipse,使用快捷键“ctrl+H”打开文件搜索对话框,或者点击“search”标签打开文件搜索对话框。2、然后选择“FileSearch”标签,在Containingtext…

    2025年7月1日
    4

发表回复

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

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