java文件流编码

JAVA

Posted by 蒋为 on April 9, 2017

记录

BufferedReader br = new BufferedReader(new FileReader(fileName));  
String line = null;  
while ((line = br.readLine()) != null) {   
      System.out.println(line);
}  
br.close();  

原因:Java读取数据流的时候,如果没有指定数据流的编码方式,则将使用本地环境中的默认字符集。

So!

BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(fileName),"UTF-8"));  
String line = null;  
while ((line = br.readLine()) != null) {  
      System.out.println(line);  //乱码消失

}  
br.close();