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

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

问题描述

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

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

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

我尝试使用以下方法:

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.

推荐答案

我使用了这个库,类似于 jchardet 用于检测 Java 中的编码:https://github.com/albfernandez/juniversalchardet

I have used this library, similar to jchardet for detecting encoding in Java: https://github.com/albfernandez/juniversalchardet

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

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