Java-IO之InputStreamReader和OutputStreamWriter
public class StreamConverter { private static final String FileName = "file.txt"; private static final String CharsetName = "utf-8"; //private static final String CharsetName = "gb2312"; public static void main(String[] args) { testWrite(); testRead(); } /** * OutputStreamWriter 演示函数 * */ private static void testWrite() { try { // 创建文件“file.txt”对应File对象 File file = new File(FileName); // 创建FileOutputStream对应OutputStreamWriter:将字节流转换为字符流,即写入out1的数据会自动由字节转换为字符。 OutputStreamWriter out1 = new OutputStreamWriter(new FileOutputStream(file), CharsetName); // 写入10个汉字 out1.write("字节流转为字符流示例"); // 向“文件中”写入"0123456789"+换行符 out1.write("0123456789\n"); out1.close(); } catch(IOException e) { e.printStackTrace(); } } /** * InputStreamReader 演示程序 */ private static void testRead() { try { // 方法1:新建FileInputStream对象 // 新建文件“file.txt”对应File对象 File file = new File(FileName); InputStreamReader in1 = new InputStreamReader(new FileInputStream(file), CharsetName); // 测试read(),从中读取一个字符 char c1 = (char)in1.read(); System.out.println("c1="+c1); // 测试skip(long byteCount),跳过4个字符 in1.skip(6); // 测试read(char[] cbuf, int off, int len) char[] buf = new char[10]; in1.read(buf, 0, buf.length); System.out.println("buf="+(new String(buf))); in1.close(); } catch(IOException e) { e.printStackTrace(); } } }
基于JDK8的InputStreamReader的源码:
public class InputStreamReader extends Reader { private final StreamDecoder sd; //构造函数,使用平台默认的字符类型 public InputStreamReader(InputStream in) { super(in); try { sd = StreamDecoder.forInputStreamReader(in, this, (String)null); // ## check lock object } catch (UnsupportedEncodingException e) { // The default encoding should always be available throw new Error(e); } } /** * Creates an InputStreamReader that uses the named charset. * * @param in * An InputStream * * @param charsetName * The name of a supported * {@link java.nio.charset.Charset charset} * * @exception UnsupportedEncodingException * If the named charset is not supported */ //构造函数,使用提供的字符类型 public InputStreamReader(InputStream in, String charsetName)throws UnsupportedEncodingException { super(in); if (charsetName == null) throw new NullPointerException("charsetName"); sd = StreamDecoder.forInputStreamReader(in, this, charsetName); } //构造函数,使用提供的字符类型 public InputStreamReader(InputStream in, Charset cs) { super(in); if (cs == null) throw new NullPointerException("charset"); sd = StreamDecoder.forInputStreamReader(in, this, cs); } //使用提供的字符译码器 public InputStreamReader(InputStream in, CharsetDecoder dec) { super(in); if (dec == null) throw new NullPointerException("charset decoder"); sd = StreamDecoder.forInputStreamReader(in, this, dec); } //返回字符编码 public String getEncoding() { return sd.getEncoding(); } //读单个字符 public int read() throws IOException { return sd.read(); } //读字符到cbuf中 public int read(char cbuf[], int offset, int length) throws IOException { return sd.read(cbuf, offset, length); } //是否准备好读 public boolean ready() throws IOException { return sd.ready(); } //关闭资源 public void close() throws IOException { sd.close(); } }
基于JDK8的OutputStreamWriter的源码:
public class OutputStreamWriter extends Writer { private final StreamEncoder se; //构造函数,使用被命名的字符集 public OutputStreamWriter(OutputStream out, String charsetName)throws UnsupportedEncodingException { super(out); if (charsetName == null) throw new NullPointerException("charsetName"); se = StreamEncoder.forOutputStreamWriter(out, this, charsetName); } //构造函数,使用系统默认的字符集 public OutputStreamWriter(OutputStream out) { super(out); try { se = StreamEncoder.forOutputStreamWriter(out, this, (String)null); } catch (UnsupportedEncodingException e) { throw new Error(e); } } //构造函数,使用给定的字符集 public OutputStreamWriter(OutputStream out, Charset cs) { super(out); if (cs == null) throw new NullPointerException("charset"); se = StreamEncoder.forOutputStreamWriter(out, this, cs); } //构造函数,使用给定的字符编码器 public OutputStreamWriter(OutputStream out, CharsetEncoder enc) { super(out); if (enc == null) throw new NullPointerException("charset encoder"); se = StreamEncoder.forOutputStreamWriter(out, this, enc); } //获得编码器 public String getEncoding() { return se.getEncoding(); } //刷新缓冲区, void flushBuffer() throws IOException { se.flushBuffer(); } //写单个字符 public void write(int c) throws IOException { se.write(c); } //写字符数组的一部分 public void write(char cbuf[], int off, int len) throws IOException { se.write(cbuf, off, len); } //写字符串的一部分 public void write(String str, int off, int len) throws IOException { se.write(str, off, len); } //刷新流 public void flush() throws IOException { se.flush(); } //关闭资源 public void close() throws IOException { se.close(); } }
Java-IO之InputStreamReader和OutputStreamWriter的更多相关文章
- java io系列21之 InputStreamReader和OutputStreamWriter
InputStreamReader和OutputStreamWriter 是字节流通向字符流的桥梁:它使用指定的 charset 读写字节并将其解码为字符.InputStreamReader 的作用是 ...
- Java API —— 编码 & IO流( InputStreamReader & OutputStreamWriter & FileReader & FileWriter & BufferedReader & BufferedWriter )
1.编码 1)编码表概述 由字符及其对应的数值组成的一张表 2)常见编码表 · ASCII/Unicode 字符集:ASCII是美国标准信息交换码,用一 ...
- 【java】io流之字节流转为字符流:java.io.OutputStreamWriter和java.io.InputStreamReader
package 文件操作; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; impo ...
- Java IO流学习总结五:转换流-InputStreamReader、OutputStreamWriter
类的继承关系 Reader |__ BufferedReader.StringReader.InputStreamReader |__ FileReader Writer |__ BufferedWr ...
- 转换流读写操作 java.io.OutputStreamWriter ,java.io.InputStreamReader
package seday07; import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStr ...
- Java IO: InputStreamReader和OutputStreamWriter
作者: Jakob Jenkov 译者: 李璟(jlee381344197@gmail.com) 本章节将简要介绍InputStreamReader和OutputStreamWriter.细心的读者可 ...
- Java之IO(十三)File、Filter、Piped、String和InputStreamReader与OutputStreamWriter
转载请注明原出处:http://www.cnblogs.com/lighten/p/7264196.html 1.前言 断更一段时间,计划果然赶不上变化(还是太懒...).这次一次性将剩余的5组字符流 ...
- java基础知识回顾之javaIO类---InputStreamReader和OutputStreamWriter转化流
InputStreamReader:是字节流通向字符流的桥梁: OutputStreamWriter 是字符流通向字节流的桥梁: package com.lp.ecjtu; import java.i ...
- J07-Java IO流总结七 《 InputStreamReader和OutputStreamWriter 》
前面在介绍FileReader和FileWriter的时候有说到,FileReader的读取字符功能,以及FileWriter的写出字符的功能,都不是它们自己实现的,而是,它们分别继承了InputSt ...
- io流之转换流InputStreamReader、OutputStreamWriter
例子程序: package io; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.File ...
随机推荐
- es6新增
首先要说let,他是只在代码块中执行的变量,例如: { let a = 10; var b = 1;}console.log(a);//definedconsole.log(b);//1 ...
- VMWare 学习目录
Linux介绍 Linux入门--个人感想 Google怎么用linux 初入Linux Windows XP硬盘安装Ubuntu 12.04双系统图文详解 实例讲解虚拟机3种网络模式(桥接.nat. ...
- WPF 窗口居中 & 变更触发机制
窗口居中 & 变更触发机制 解决: 1.单实例窗口,窗口每次隐藏后再显示时,位置居中显示 2.多屏幕下单实例窗口,当父窗口移动到其它屏幕时,单实例窗口再次弹出时,位置才更新到父窗口屏幕. 3. ...
- WPF TextBlock 判断 isTextTrimmed 文本是否超出
WPF TextBlock 设置TextTrimming情况下 判断 isTextTrimmed(Text 文本是否超出 是否出现了省略号) private bool HasTextTrimmed(T ...
- scan函数用法详解
scan函数: scan(s,n,"char")表示从字串string中以char为分隔符提取第n个字串 功能(function):从字符表达式s中搜取给定的n个单词语法(synt ...
- 解读Batch Normalization
原文转自:http://blog.csdn.net/shuzfan/article/details/50723877 本次所讲的内容为Batch Normalization,简称BN,来源于<B ...
- hiredis异步接口封装并导出到Lua
hiredis异步接口封装并导出到Lua(金庆的专栏 2017.1)hiredis 不支持 Windows, Windows 下使用 wasppdotorg / hiredis-for-windows ...
- A discussion of Dead Connection Detection, Resource Limits, V$SESSION, V$PROCESS and OS processes
A discussion of Dead Connection Detection, Resource Limits, V$SESSION, V$PROCESS and OS processes (文 ...
- Bootstrap3 栅格系统-实例:流式布局容器
如何让小屏幕设备上所有列不堆叠在一起,使用针对超小屏幕和中等屏幕设备所定义的类就可以做到,即 .col-xs-* 和 .col-md-*.举例: <!-- Stack the columns o ...
- leetcode之Largest Rectangle in Histogram
问题来源:Largest Rectangle in Histogram 问题描述:给定一个长度为n的直方图,我们可以在直方图高低不同的长方形之间画一个更大的长方形,求该长方形的最大面积.例如,给定下述 ...