java: InputStreamReader将字节的输入流变成字符的输入流,OutputStreamWriter将字符的输出流变成字节的输出流
Posted 穆晟铭
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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将字符的输出流变成字节的输出流的主要内容,如果未能解决你的问题,请参考以下文章
Java基础InputStream InputStreamReader和BufferedReader