首页
学习
活动
专区
圈层
工具
发布

java IO流之四 使用转换流InputStreamReader和OutputStreamWriter

http://blog.csdn.net/a107494639/article/details/7586744

当字节流和字符流之间需要转化的时候,或者要对字节数据进行编码转换的时候,就需要使用转换流

[java] view plain copy

  1. package org.example.io;  
  2. import java.io.File;  
  3. import java.io.FileInputStream;  
  4. import java.io.FileOutputStream;  
  5. import java.io.InputStreamReader;  
  6. import java.io.OutputStreamWriter;  
  7. public class TestStreamReader {  
  8. public static void main(String[] args) throws Exception {  
  9.         File file = new File("E:\\b.txt");  
  10. if (!file.exists()) {  
  11.             file.createNewFile();  
  12.         }  
  13.         OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(file), "GBK");  
  14.         out.write("hello world,你好世界");  
  15.         out.close();  
  16.         InputStreamReader in = new InputStreamReader(new FileInputStream(file), "gbk");  
  17. char[] cc = new char[1024];  
  18. int n = 0;  
  19.         String str = "";  
  20. while ((n = in.read(cc)) != -1) {  
  21.             str += new String(cc, 0, n);  
  22.         }  
  23.         in.close();  
  24.         System.out.println(str);  
  25.     }  
  26. }  
举报
领券