产生乱码的原因是因为,win7系统的编码和linux系统编码不一致引起的。因为IO操作流和系统有交互,所以产生了乱码。
在读取文本时的处理:
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String tempContent = "";
while ((tempContent = reader.readLine()) != null) {
byte[] bytes = tempContent.getBytes("UTF-8");
tempContent = new String(bytes,"UTF-8");
formerInfo.append(tempContent.trim()+"\n");
}
}
在linux中通过:
iconv -f GB2312 -t utf-8 text.txt > text-utf-8.txt
将文本将换成utf-8的编码。
前提是你linux系统的编码是utf-8.可以使用命令: echo $LANG查看系统编码。
可能通过/etc/sysconfig/i18n查看