最近遇到了一个小麻烦,就是在eclipse环境中读写UTF-8格式的txt文件时很正常,但是当导出成jar包后,通过点击来读写文件时出现了部分中文乱码问题。
解决办法:
开始时用的是FileReader和FileWriter来读写的,后来改成用InputStreamReader和OutputStreamWriter后问题解决
关键代码:
1
2
3
4
5
6
7
8
|
BufferedReader reader; BufferedWriter writer; //reader = new BufferedReader(new FileReader(new File(filedir,filename))); reader = new BufferedReader( new InputStreamReader( new FileInputStream( new File(filedir,filename)), "UTF-8" ));
//writer = new BufferedWriter(new FileWriter(new File(filedir,emailDomain+".txt"),true)); writer = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( new File(filedir,emailDomain+ ".txt" ), true ), "UTF-8" ));
|
(PS:不用管代码中的具体文件路径和文件名,看懂思路即可)
本文转自 pangfc 51CTO博客,原文链接:http://blog.51cto.com/983836259/1661761,如需转载请自行联系原作者