解決BufferedReader读取UTF-8文件中文乱码(转)

读取txt文件乱码 

复制代码
Java代码
1.BufferedReader read = new BufferedReader(new FileReader(new File(filename)));  



解决办法: 



Java代码 
1.InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");  
2.BufferedReader read = new BufferedReader(isr);  
复制代码



因为InputStreamReader和BufferedReader 都继承自Reader,而BufferedReader 的构造器又是Reader. 



Writer也是一样的处理 

Java代码 
1.FileOutputStream writerStream = new FileOutputStream(filePath, true);  
2.BufferedWriter oWriter = new BufferedWriter(new OutputStreamWriter(writerStream, "UTF-8"));  

 

posted @ 2020-12-21 23:16  牧之丨  阅读(264)  评论(0编辑  收藏  举报