现在有一需求,有一文件是utf-8编码格式其内容有乱码,如果我将此文件用 文本编辑器 打开并另存为且以ANSI格式保存后,其乱码变为正确的中文。但是我用java写了一个程序转换(即以utf-8读取,以gb2312格式写入另一文件),新的文件还是乱码!
想请高手指点指点,谢谢
我的程序是参考 http://dali.iteye.com/blog/73860 文章!
想请高手指点指点,谢谢
我的程序是参考 http://dali.iteye.com/blog/73860 文章!
BufferedReader br = null;
;
BufferedWriter bw = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(
srcFileName), "utf-8"));
bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(destFileName), "gbk"));
String line = null;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
}
} catch (Exception e) {
} finally {
try {
if (br != null)
br.close();
if (bw != null)
bw.close();
} catch (IOException e) {
}
}
}
UTF-8表示汉字要3字节,GB2312只需要2字节。
ANSI码只能标识一个字节的127个字符,UTF-8,GB2312转换成它肯定会丢失数据无法显示了。