Java文本文件编码

我有一个文本文件,它可以是ANSI(带有ISO-8859-2字符集),UTF-8,UCS-2 Big或Little Endian。

有没有办法检测文件的编码以正确读取它?

或者是否可以在不提供编码的情况下读取文件? (它按原样读取文件)

(有几个程序可以检测和转换文本文件的编码/格式。)

UTF-8和UCS-2 / UTF-16可以通过文件开头的字节顺序标记合理地区分。 如果存在,那么该文件处于该编码状态是一个相当不错的选择 – 但这并不是一个确定的问题。 您可能还会发现该文件位于其中一种编码中,但没有字节顺序标记。

我对ISO-8859-2了解不多,但如果几乎每个文件都是该编码中的有效文本文件,我都不会感到惊讶。 你能做的最好的就是启发式检查。 实际上, 维基百科页面谈论它会表明只有字节0x7f无效。

不知道是否“按原样”读取文件并将文本输出 – 文件是一个字节序列,因此您必须应用字符编码才能将这些字节解码为字符。

是的,有许多方法可以进行字符编码检测,特别是在Java中。 看一下基于Mozilla算法的jchardet 。 还有cpdetector和IBM的一个名为ICU4j的项目。 我会看看后者,因为它似乎比其他两个更可靠。 它们基于二进制文件的统计分析工作,ICU4j还将提供它检测到的字符编码的置信度,因此您可以在上面的情况下使用它。 它工作得很好。

你可以使用ICU4J( http://icu-project.org/apiref/icu4j/

这是我的代码:

String charset = "ISO-8859-1"; //Default chartset, put whatever you want byte[] fileContent = null; FileInputStream fin = null; //create FileInputStream object fin = new FileInputStream(file.getPath()); /* * Create byte array large enough to hold the content of the file. * Use File.length to determine size of the file in bytes. */ fileContent = new byte[(int) file.length()]; /* * To read content of the file in byte array, use * int read(byte[] byteArray) method of java FileInputStream class. * */ fin.read(fileContent); byte[] data = fileContent; CharsetDetector detector = new CharsetDetector(); detector.setText(data); CharsetMatch cm = detector.detect(); if (cm != null) { int confidence = cm.getConfidence(); System.out.println("Encoding: " + cm.getName() + " - Confidence: " + confidence + "%"); //Here you have the encode name and the confidence //In my case if the confidence is > 50 I return the encode, else I return the default value if (confidence > 50) { charset = cm.getName(); } } 

记得把所有尝试捕获需要它。

我希望这适合你。

如果您的文本文件是正确创建的Unicode文本文件,则字节顺序标记(BOM)应该告诉您所需的所有信息。 有关BOM的更多详细信息,请参见此处

如果不是那么你将不得不使用一些编码检测库。