Java核心类库-IO-转换流(InputStreamReader-OutputStreamWriter)
转换流:把字节流转换成字符输入流
InputStreamReader:把字节输入流转成字符输入流
OutputStreamWriter:把字节输出流转换成字符输出流
为什么有字节转字符流,没有字符转字节流
字节流可以操作一切文件(纯文本文件/二进制文件)
字符流是用来操作中文纯文本使用的,本身是对自己的增强。
1 public class StreamCopyDemo { 2 public static void main(String[] args) throws Exception { 3 File srcFile = new File("stream.txt"); 4 File destFile = new File("stream2.txt"); 5 Reader in = new InputStreamReader(new FileInputStream(srcFile),"UTF-8"); 6 Writer out = new OutputStreamWriter(new FileOutputStream(destFile),"UTF-8"); 7 //接下来操作字符流 8 char[] buffer = new char[1024]; 9 int len = -1; 10 while ((len = in.read(buffer)) != -1){ 11 out.write(buffer,0,len); 12 } 13 in.close(); 14 out.close(); 15 } 16 }