java filereader默认编码,Java FileReader编码问题

I tried to use java.io.FileReader to read some text files and convert them into a string, but I found the result is wrong encoded and not readable at all.

Here's my environment:

Windows 2003, OS encoding: CP1252

Java 5.0

My files are UTF-8 encoded or CP1252 encoded, and some of them (UTF-8 encoded files) may contain Chinese (non-Latin) characters.

I use the following code to do my work:

private static String readFileAsString(String filePath)

throws java.io.IOException{

StringBuffer fileData = new StringBuffer(1000);

FileReader reader = new FileReader(filePath);

//System.out.println(reader.getEncoding());

BufferedReader reader = new BufferedReader(reader);

char[] buf = new char[1024];

int numRead=0;

while((numRead=reader.read(buf)) != -1){

String readData = String.valueOf(buf, 0, numRead);

fileData.append(readData);

buf = new char[1024];

}

reader.close();

return fileData.toString();

}

The above code doesn't work. I found the FileReader's encoding is CP1252 even if the text is UTF-8 encoded. But the JavaDoc of java.io.FileReader says that:

The constructors of this class assume

that the default character encoding

and the default byte-buffer size are

appropriate.

Does this mean that I am not required to set character encoding by myself if I am using FileReader? But I did get wrong encoded data currently, what's the correct way to deal with my situtaion? Thanks.

解决方案

Yes, you need to specify the encoding of the file you want to read.

Yes, this means that you have to know the encoding of the file you want to read.

No, there is no general way to guess the encoding of any given "plain text" file.

The constructors of FileReader always use the platform default encoding which is generally a bad idea.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值