Java:如何确定流的正确字符集编码 [英] Java : How to determine the correct charset encoding of a stream

查看:142
本文介绍了Java:如何确定流的正确字符集编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考以下线程:
Java应用程序:无法正确读取iso-8859-1编码文件

什么是编程确定正确的字符集编码的最佳方法输入流/文件?

What is the best way to programatically determine the correct charset encoding of an inputstream/file ?

我已尝试使用以下内容:

I have tried using the following:

File in =  new File(args[0]);
InputStreamReader r = new InputStreamReader(new FileInputStream(in));
System.out.println(r.getEncoding());

但是,在我知道要使用ISO8859_1进行编码的文件中,上述代码产生ASCII正确,并且不允许我正确地将文件的内容呈现回控制台。

But on a file which I know to be encoded with ISO8859_1 the above code yields ASCII, which is not correct, and does not allow me to correctly render the content of the file back to the console.

推荐答案

我已经使用这个库,类似于用于检测Java编码的jchardet:
http://code.google.com/ p / juniversalchardet /

I have used this library, similar to jchardet for detecting encoding in Java: http://code.google.com/p/juniversalchardet/

这篇关于Java:如何确定流的正确字符集编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆