文本文件的輸入輸出流(這裏的重點在於字符集問題)

    如何以指定字符集讀取、寫出文本文件?如下是我寫得Demo java

public class Charset { utf-8

 public static void main(String[] args) throws IOException {
   test();
 }
 public static void test() throws IOException {
  String srcPath = "c:/HelloWorld2.java";
  InputStream is = new FileInputStream(srcPath);
  InputStreamReader isr = new InputStreamReader(is,"utf-8");//默認GBK字符集
  
  
  String destPath = "c:/HelloWorld5.java";
  OutputStream os = new FileOutputStream(destPath);
  OutputStreamWriter osw = new OutputStreamWriter(os,"utf-8");
  
  char[] cbuf = new char[1024];
  int len = 0;
  while(-1!=(len=isr.read(cbuf))){
   System.out.println(isr.getEncoding());
  // System.out.println(Arrays.toString(cbuf));
   osw.write(cbuf, 0, len);
  }
  
  osw.flush();
  is.close();
  os.close();
 }
 
} get

相關文章
相關標籤/搜索