最近遇到了一个小麻烦,就是在eclipse环境中读写UTF-8格式的txt文件时很正常,但是当导出成jar包后,通过点击来读写文件时出现了部分中文乱码问题。
解决办法:
开始时用的是FileReader和FileWriter来读写的,后来改成用InputStreamReader和OutputStreamWriter后问题解决
关键代码:
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:不用管代码中的具体文件路径和文件名,看懂思路即可)
时间: 2025-01-14 01:45:52