参考以下线程: Java App : Unable to read iso-8859-1 encoded file correctly
以编程方式确定输入流/文件的正确字符集编码的最佳方法是什么?
我尝试使用以下内容:
File in = new File(args[0]);
InputStreamReader r = new InputStreamReader(new FileInputStream(in));
System.out.println(r.getEncoding());
但是在我知道用 ISO8859_1 编码的文件上,上面的代码产生了 ASCII,这是不正确的,并且不允许我将文件的内容正确地呈现回控制台。
原文由 Joel 发布,翻译遵循 CC BY-SA 4.0 许可协议
我使用了这个库,类似于 jchardet 用于检测 Java 中的编码: https ://github.com/albfernandez/juniversalchardet