码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-07-23 15:30:27      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:read   logs   fileread   接收   out   缓冲   stringbu   puts   write   

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将字符的输出流变成字节的输出流

标签:read   logs   fileread   接收   out   缓冲   stringbu   puts   write   

原文地址:http://www.cnblogs.com/achengmu/p/7224602.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!