Java小问题合集-持续收集


1. eclipse中java项目导出成jar包后读写UTF-8文件中文乱码问题

【解决方法】开始时用的是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"));

对于其中的FileOutputStream,如果第二个参数为 true,则将字节写入文件末尾处,而不是写入文件开始处(即重写文件)。

你可能感兴趣的:(Java小问题合集-持续收集)