文本文件编码为utf-8,eclipse设置的工作环境为gbk编码
输出的文本文件,比原文件多了一个?代码如下public class Main {
public static void main(String[] args) throws IOException {
File file = new File("f:\\temp\\1.txt");
InputStreamReader isr = new InputStreamReader(new FileInputStream(file),
"utf-8");
//FileReader fr = new FileReader(file);
FileWriter fw = new FileWriter("f:\\temp\\2.txt");
//OutputStreamWriter fw = new OutputStreamWriter(new FileOutputStream("f:\\temp\\2.txt"),"utf-8");
// byte[] b = new byte[2*1024];
int l;
while ((l = isr.read()) != -1) {
fw.write(l);
}
fw.close();
isr.close();
}
}
1 回答
已采纳

冥幻影
TA贡献5条经验 获得超0个赞
FileWriter在写出文件时是以系统默认的编码格式输出的,你输入文本指定为utf-8,但是你指定输出文件输出使用不同编码,所以多了一个问号,因此对于一些不同编码格式容易产生乱码,尤其对于中文字符, 在使用时应格外留意。为了解决这个问题,可以使用OutputStreamWriter方法并且指定编码。
添加回答
举报
0/150
提交
取消