Java_io体系之OutputStreamWriter、InputStreamReader简介、走进源码及示例――17(四)

2014-11-24 07:53:52 · 作者: · 浏览: 2
高效率、一般结合BufferedReader使用。 * * @version 1.1, 13/11/16 * @author andyChen */ public class InputStreamReader extends Reader { //可以使用指定编码读取字节的本质。使用其将byte解码成字符串返回。 //对应的OutputStreamWriter中的StreamEncoder将字节编码成字符写入out中。 //对使用OutputStreamWriter写入的out读取时、注意编码。否则会造成乱码。 private final StreamDecoder sd; /** * 使用默认编码将字节输入流InputStream in转换成字符输入流 */ 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转换成isr. */ public InputStreamReader(InputStream in, Charset cs) { super(in); if (cs == null) throw new NullPointerException("charset"); sd = StreamDecoder.forInputStreamReader(in, this, cs); } /** * 使用指定的字符编码将in转换成isr */ 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(); } }

4、实例演示:


package com.chy.io.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

public class InputStreamReaderTest {
	private static File file = new File("D:" + File.separator + "osw.txt");
	private static FileInputStream fis;
	static{
		try {
			fis = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public static void testISRDefaultEncoder()throws IOException{
		//使用默认编码读取文件中的第一“威尔杰克逊”
		InputStreamReader isrDefault = new InputStreamReader(fis);
		BufferedReader bisDefault = new BufferedReader(isrDefault);
		System.out.println("isr encoding : " + isrDefault.getEncoding() + "======str: " + bisDefault.readLine());
		bisDefault.close();
	}
	
	public static void testISRGBK() throws IOException{
		//使用GBK读取第一个“威尔杰克逊”
		InputStreamReader isrGBK = new InputStreamReader(fis, "GBK");
		BufferedReader bisGBK = new BufferedReader(isrGBK);
		System.out.println("isr encoding : " + isrGBK.getEncoding() + "=====str: " + bisGBK.readLine());
		bisGBK.close();
	}
	
	public static void testISRUTF8() throws IOException{
		//使用UTF-8读取文件中的第一个“威尔杰克逊”
		InputStreamReader isrUFT8 = new InputStreamReader(fis, "UTF-8");
		BufferedReader bisUFT8 = new BufferedReader(isrUFT8);
		System.out.println("isr encoding : " + isrUFT8.getEncoding() + "=====str: " + bisUFT8.readLine());
		bisUFT8.close();
	}
	
	public static void main(String[] args) throws IOException{
		testISRDefaultEncoder();
		//testISRGBK();
		//testISRUTF8();
		
	}
}


总结:


OutputStreamWriter、InputStreamReader分别为InputStream、OutputStream的低级输入输出流提供将字节转换成字符的桥梁、他们只是外边的一个门面、真正的核心