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


相关推荐

  • 微信小程序后端开发流程_微信小程序开发入门

    微信小程序后端开发流程_微信小程序开发入门微信小程序开发流程记录一、代码处理(一)微信小程序(前端显示)微信小程序项目的架构部分特点说明(二)后台服务器(数据交互)需要的环境特别注意二、项目部署(一)Wampserver的设置(二)域名的获取(三)小程序官方网站上的设置一、代码处理最近一大学同窗开了家自习室,需要一个自习室的预约微信小程序,又恰好手上没有什么代码项目在写,本着少一事不如多一事的原则,尝试了第一次的微信小程序开发。白天要上班,只能利用晚上时间编写,反正他也不着急开业,于是前前后后用了大概半个月的时间,故做记录。刚开始写,确实没什

    2022年9月2日
    6
  • java面试题汇总(一)_java框架面试题

    java面试题汇总(一)_java框架面试题SQL语句面试大全,两万字详解,专栏持续更新

    2022年8月29日
    1
  • 写辅助脚本违法吗_网络游戏里的成功几率

    写辅助脚本违法吗_网络游戏里的成功几率转至http://www.cppblog.com/elva/archive/2008/02/19/42924.html一、前言  所谓游戏外挂,其实是一种游戏外辅程序,它可以协助玩家自动产生游戏动作、修改游戏网络数据包以及修改游戏内存数据等,以实现玩家用最少的时间和金钱去完成功力升级和过关斩将。虽然,现在对游戏外挂程序的“合法”身份众说纷纭,在这里我不想对此发表任何个人意见,让时间…

    2022年10月8日
    0
  • 目标检测综述_通用目标检测

    目标检测综述_通用目标检测前言图片分类任务我们已经熟悉了,就是算法对其中的对象进行分类。而今天我们要了解构建神经网络的另一个问题,即目标检测问题。这意味着,我们不仅要用算法判断图片中是不是一辆汽车,还要在图片中标记出它的位置,用边框或红色方框把汽车圈起来,这就是目标检测问题。其中“定位”的意思是判断汽车在图片中的具体位置。近几年来,目标检测算法取得了很大的突破。比较流行的算法可以分为两类,一类是基于Regio…

    2022年10月13日
    1
  • Eclipse代码自动补全功能

    Eclipse代码自动补全功能相信用过IntelliJIDEA的同学都知道哈,输出语句是可以直接sout回车得到System.out.println();这语句哈,最近呢,想用eclipse写写java,发现打出输出语句不是那么快,于是就上网查询了相关资料,期间发现个很棒的教程。教程如下:1.打开eclipse找到顶部Windows那里(如图)2.点击Windows那里,然后在弹出的框中选择Preference(如图)3.点开后在弹出界面找到java(如图,找到后点击java左边的那个箭头(箭头额)4.第三

    2022年5月31日
    41
  • A 股历年三大财务报表 API 接口「建议收藏」

    A股历年三大财务报表历年所有财报数据,全量A股数据,最全三大财报数据。1.产品功能支持所有A股全量三大财报数据查询;分别包括资产负债表、利润表、现金流量表数据;返回70多项财务指标;多数据源清洗整合,百万级数据毫秒级返回;全接口支持HTTPS(TLSv1.0/v1.1/v1.2/v1.3);全面兼容AppleATS;全国多节点CDN部署;接口极速响应,多台服务器构建API接口负载均衡。2.API文档接口详情:https://www.

    2022年4月17日
    87

发表回复

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

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