InputStreamReader 和 OutputStreamWriter类用法简介,及演示。

时间:2025-03-23 09:30:10

 InputStreamReader 和 OutputStreamWriter类用法简介。

一、InputStreamReader类

InputStreamReader 将字节流转换为字符流。是字节流通向字符流的桥梁。如果不指定字符集编码,该解码过程将使用平台默认的字符编码,如:GBK。

构造方法:

InputStreamReader isr = new InputStreamReader(InputStream in);//构造一个默认编码集的InputStreamReader类

InputStreamReader isr = new InputStreamReader(InputStream in,String charsetName);//构造一个指定编码集的InputStreamReader类。

参数 in对象通过 InputStream in = ;获得。//读取键盘上的数据。

                                或者    InputStream in = new FileInputStream(String fileName);//读取文件中的数据。可以看出FileInputStream 为InputStream的子类。

主要方法:int read();//读取单个字符。

                  int read(char []cbuf);//将读取到的字符存到数组中。返回读取的字符数。

public static void transReadNoBuf() throws IOException {
		/**
		 * 没有缓冲区,只能使用read()方法。
		 */
		//读取字节流
//		InputStream in = ;//读取键盘的输入。
		InputStream in = new FileInputStream("D:\\");//读取文件的数据。
		//将字节流向字符流的转换。要启用从字节到字符的有效转换,可以提前从底层流读取更多的字节.
		InputStreamReader isr = new InputStreamReader(in);//读取
//		InputStreamReader isr = new InputStreamReader(new FileInputStream("D:\\"));//综合到一句。
			
		char []cha = new char[1024];
		int len = (cha);
		(new String(cha,0,len));
		();

	}
	public static void transReadByBuf() throws IOException {
		/**
		 * 使用缓冲区 可以使用缓冲区对象的 read() 和  readLine()方法。
		 */
		//读取字节流
//		InputStream in = ;//读取键盘上的数据
		InputStream in = new FileInputStream("D:\\");//读取文件上的数据。
		//将字节流向字符流的转换。
		InputStreamReader isr = new InputStreamReader(in);//读取
		//创建字符流缓冲区
		BufferedReader bufr = new BufferedReader(isr);//缓冲
//		BufferedReader bufr = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\")));可以综合到一句。
	/*	int ch =0;
		ch = ();
		((char)ch);*/
		String line = null;
		while((line = ())!=null){
			(line);
		}
		();
	}


 

二、OutputStreamWriter类

OutputStreamWriter 将字符流转换为字节流。是字符流通向字节流的桥梁。如果不指定字符集编码,该解码过程将使用平台默认的字符编码,如:GBK。

构造方法:

OutputStreamWriter osw = new OutputStreamWriter(OutputStream out);//构造一个默认编码集的OutputStreamWriter类

OutputStreamWriter osw = new OutputStreamWriter(OutputStream out,String charsetName);//构造一个指定编码集的OutputStreamWriter类。

参数 out对象通过 OutputStream out = ;获得。//打印到控制台上。

      或者    OutputStream out = new FileoutputStream(String fileName);//输出到文件中。可以看出FileoutputStream 为outputStream的子类。

主要方法:void write(int c);//将单个字符写入。

                  viod write(String str,int off,int len);//将字符串某部分写入。

                  void flush();//将该流中的缓冲数据刷到目的地中去。

 

	public static void transWriteNoBuf() throws IOException {
		OutputStream out = ;//打印到控制台
//		OutputStream out = new FileOutputStream("D:\\");//打印到文件
		OutputStreamWriter osr = new OutputStreamWriter(out);//输出
//		OutputStreamWriter osr = new OutputStreamWriter(new FileOutputStream("D:\\"));//两句可以综合到一句。
//		int ch = 97;//a
//		int ch = 20320;//你
//		(ch);
		String str = "你好吗?";//你好吗?
		(str);
		();
		();
	}
	public static void transWriteByBuf() throws IOException {
//		OutputStream out = ;//打印到控制台。
		OutputStream out = new FileOutputStream("D:\\");//打印到文件。
		OutputStreamWriter osr = new OutputStreamWriter(out);//输出
//		OutputStreamWriter osr = new OutputStreamWriter(new FileOutputStream("D:\\"));//综合到一句。
		BufferedWriter bufw = new BufferedWriter(osr);//缓冲
//		int ch = 97;//a
//		int ch = 20320;//你
//		(ch);
		String str = "你好吗?\r\n我很好!";//你好吗?
		(str);
		();
		();
	}

 

 流转换程序1:

package IOtest;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

public class TransStreamtest {

	/**
	 * 主要的类:	in1,	InputStream
	 * 						创建对象 InputStream in = ;
	 * 				in2,	InputStreamReader  没有readLine()方法
	 * 						主要方法:
	 * 							read()读取单个字符,一个汉字也为一个字符。
	 * 							read(char[] cbuf)将字符读入数组。
	 * 							close().关闭此流和相关联资源。
	 * 				in3,	BufferedReader     有read(),readLine()方法。
	 * 				out1,	OutputStream
	 * 						创建对象 OutputStream in = ;
	 * 				out2,	OutputStreamWriter  
	 * 						主要方法:
	 * 							write(int c)//写入单个字符。
	 * 							write(char[] cbuf,int off,int len)//写入数组的某一部分
	 * 							write(String str,int off,int len)//写入字符串烦人某一部分。
	 * 							flush();//刷新该流中的缓冲。
	 * 							close();
	 * 				out3,	BufferedWriteer     有Write(int ch),newLine()方法。
	 * 
	 * 			
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
//		transReadByBuf();
//		transReadNoBuf();
		transWriteNoBuf();
//		transWriteByBuf();
		
	}

	public static void transWriteNoBuf() throws IOException {
		OutputStream out = ;//打印到控制台
//		OutputStream out = new FileOutputStream("D:\\");//打印到文件
		OutputStreamWriter osr = new OutputStreamWriter(out);//输出
//		OutputStreamWriter osr = new OutputStreamWriter(new FileOutputStream("D:\\"));//两句可以综合到一句。
//		int ch = 97;//a
//		int ch = 20320;//你
//		(ch);
		String str = "你好吗?";//你好吗?
		(str);
		();
		();
	}
		public static void transWriteByBuf() throws IOException {
//			OutputStream out = ;//打印到控制台。
			OutputStream out = new FileOutputStream("D:\\");//打印到文件。
			OutputStreamWriter osr = new OutputStreamWriter(out);//输出
//			OutputStreamWriter osr = new OutputStreamWriter(new FileOutputStream("D:\\"));//综合到一句。
			BufferedWriter bufw = new BufferedWriter(osr);//缓冲
//		int ch = 97;//a
//		int ch = 20320;//你
//		(ch);
			String str = "你好吗?\r\n我很好!";//你好吗?
			(str);
			();
			();
	}

	
	public static void transReadNoBuf() throws IOException {
		/**
		 * 没有缓冲区,只能使用read()方法。
		 */
		//读取字节流
//		InputStream in = ;//读取键盘的输入。
		InputStream in = new FileInputStream("D:\\");//读取文件的数据。
		//将字节流向字符流的转换。要启用从字节到字符的有效转换,可以提前从底层流读取更多的字节.
		InputStreamReader isr = new InputStreamReader(in);//读取
//		InputStreamReader isr = new InputStreamReader(new FileInputStream("D:\\"));//综合到一句。
			
		char []cha = new char[1024];
		int len = (cha);
		(new String(cha,0,len));
		();
	}
	
	public static void transReadByBuf() throws IOException {
		/**
		 * 使用缓冲区 可以使用缓冲区对象的 read() 和  readLine()方法。
		 */
		//读取字节流
//		InputStream in = ;//读取键盘上的数据
		InputStream in = new FileInputStream("D:\\");//读取文件上的数据。
		//将字节流向字符流的转换。
		InputStreamReader isr = new InputStreamReader(in);//读取
		//创建字符流缓冲区
		BufferedReader bufr = new BufferedReader(isr);//缓冲
//		BufferedReader bufr = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\")));可以综合到一句。
	/*	int ch =0;
		ch = ();
		((char)ch);*/
		String line = null;
		while((line = ())!=null){
			(line);
		}
		();
	}
}

 流转换程序2:

package readKey;

import ;
import ;
import ;
import ;
import ;
import ;
import ;

public class TransStreamDemo3 {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
//		writeText_1();
//		writeText_2();
//		writeText_3();
//		ReadTest_1();
//		ReadTest_2();
//		ReadTest_3();

	} 


	public static void ReadTest_3() throws IOException {
		InputStreamReader isr = new InputStreamReader(new FileInputStream("D:\\"),"UTF-8");
		char []ch = new char[20];
		int len = (ch);
		(new String(ch,0,len) );
		();	
		
	}
	public static void ReadTest_2() throws IOException {
		InputStreamReader isr = new InputStreamReader(new FileInputStream("D:\\"),"GBK");
		char []ch = new char[20];
		int len = (ch);
		(new String(ch,0,len) );
		();	
		
	}
	public static void ReadTest_1() throws IOException {
		FileReader fr = new FileReader("D:\\");
		char []ch = new char[20];
		int len = (ch);
		(new String(ch,0,len) );
		();		
	}


	public static void writeText_3() throws IOException {
		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:\\"),"UTF-8");
		("你好吗");
		();	
	}

	public static void writeText_2() throws IOException {
		FileWriter fw = new FileWriter("D:\\");
		("你好啊");
		();
	}

	public static void writeText_1() throws IOException {
		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:\\"),"GBK");
		/*
		 *和上面的等同 
		 * FileWriter fw = new FileWriter("D:\\");
		 * 操作文件的字节流 + 默认的编码表
		 */
		("你好吗");
		();
	}
}