首页 > 代码库 > java: InputStreamReader将字节的输入流变成字符的输入流,OutputStreamWriter将字符的输出流变成字节的输出流

java: InputStreamReader将字节的输入流变成字符的输入流,OutputStreamWriter将字符的输出流变成字节的输出流

InputStreamReader:将字节的输入流变成字符的输入流,

OutputStreamWriter:将字符的输出流变成字节的输出流

 

		//将缓冲区的内容读取,可以一次读取
		//可以接收键盘的输入内容:需要用到字节转字符流的方法:InputStreamReader,OutputStreamWriter
		//BufferedReader
		BufferedReader buf = null;
		
		//将字节的输入流变成字符的输入流
		//InputStreamReader 
		buf = new BufferedReader( new InputStreamReader(System.in) );
		System.out.println("请输入:");
		String str = buf.readLine();
		buf.close();
		System.out.println("内容是:" + str);
		
		buf = new BufferedReader(new FileReader("F:"+File.separator+"work"+File.separator+"work"+File.separator+"write.txt") );
		String rstr = null;
		StringBuffer strbuf = new StringBuffer();
		while( (rstr = buf.readLine()) != null )
		{
			strbuf.append(rstr);
		}
		System.out.println("输出的内容是:");
		System.out.println(strbuf);
		
		//将字符的输出流变成字节的输出流
		//OutputStreamWriter
		FileWriter fw = new FileWriter("F:"+File.separator+"work"+File.separator+"work"+File.separator+"write.txt", true);
		BufferedWriter bwriter = new BufferedWriter(fw);
		bwriter.newLine();
		bwriter.write(str);
		bwriter.flush();
		bwriter.close();
		fw.close();

  

 

java: InputStreamReader将字节的输入流变成字符的输入流,OutputStreamWriter将字符的输出流变成字节的输出流