副标题#e#
InputStreamReader和OutputStreamWriter 是字节畅通向字符流的桥梁:它利用指定的 charset 读写字节并将其解码为字符。
InputStreamReader 的浸染是将“字节输入流”转换成“字符输入流”。它担任于Reader。
OutputStreamWriter 的浸染是将“字节输出流”转换成“字符输出流”。它担任于Writer。
InputStreamReader和OutputStreamWriter源码阐明
1. InputStreamReader 源码(基于jdk1.7.40)
package java.io;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import sun.nio.cs.StreamDecoder;
// 将“字节输入流”转换成“字符输入流”
public class InputStreamReader extends Reader {
private final StreamDecoder sd;
// 按照in建设InputStreamReader,利用默认的编码
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);
}
}
// 按照in建设InputStreamReader,利用编码charsetName(编码名)
public InputStreamReader(InputStream in, String charsetName)
throws UnsupportedEncodingException
{
super(in);
if (charsetName == null)
throw new NullPointerException("charsetName");
sd = StreamDecoder.forInputStreamReader(in, this, charsetName);
}
// 按照in建设InputStreamReader,利用编码cs
public InputStreamReader(InputStream in, Charset cs) {
super(in);
if (cs == null)
throw new NullPointerException("charset");
sd = StreamDecoder.forInputStreamReader(in, this, cs);
}
// 按照in建设InputStreamReader,利用解码器dec
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();
}
// 将InputStreamReader中的数据写入cbuf中,从cbuf的offset位置开始写入,写入长度是length
public int read(char cbuf[], int offset, int length) throws IOException {
return sd.read(cbuf, offset, length);
}
// 可否从InputStreamReader中读取数据
public boolean ready() throws IOException {
return sd.ready();
}
// 封锁InputStreamReader
public void close() throws IOException {
sd.close();
}
}
#p#副标题#e#
2. OutputStreamWriter 源码(基于jdk1.7.40)
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;
// 按照out建设OutputStreamWriter,利用编码charsetName(编码名)
public OutputStreamWriter(OutputStream out, String charsetName)
throws UnsupportedEncodingException
{
super(out);
if (charsetName == null)
throw new NullPointerException("charsetName");
se = StreamEncoder.forOutputStreamWriter(out, this, charsetName);
}
// 按照out建设OutputStreamWriter,利用默认的编码
public OutputStreamWriter(OutputStream out) {
super(out);
try {
se = StreamEncoder.forOutputStreamWriter(out, this, (String)null);
} catch (UnsupportedEncodingException e) {
throw new Error(e);
}
}
// 按照out建设OutputStreamWriter,利用编码cs
// 查察本栏目
OutputStreamWriter 浸染和道理都较量简朴。
浸染就是将“字节输出流”转换成“字符输出流”。它的道理是,我们建设“字符输出流”工具时,会指定“字节输出流”以及“字符编码”。
#p#副标题#e#
示例措施
InputStreamReader和OutputStreamWriter的利用示例,参考源码(StreamConverter.java):
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;;
import java.io.InputStreamReader;
import java.io.IOException;
/**
* InputStreamReader 和 OutputStreamWriter 测试措施
*
* @author skywang
*/
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();
}
}
}
运行功效:
c1=字
buf=流示例0123456
功效说明:
(01) testWrite() 的浸染是将“内容写入到输出流”。写入的时候,会将写入的内容转换utf-8编码并写入。
(02) testRead() 的浸染是将“内容读取到输入流”。读取的时候,会将内容转换成utf-8的内容转换成字节并读出来。
生成的文件utf-8的file.txt的16进制结果图如下:

将StreamConverter.java中的CharsetName修改为"gb2312"。运行措施,出产的file.txt的16进制结果图如下:

来历:http://www.cnblogs.com/skywang12345/p/io_21.html
