在做项目时需要检测文件的编码格式,经过查阅发现icu4j可以解决这个问题。
代码如下:
package org.apache.lucene.analysis.icu.segmentation;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import com.ibm.icu.text.CharsetDetector;
import com.ibm.icu.text.CharsetMatch;
public class TestFileEncoding {
public static void main(String[] args) {
//System.getProperties().list(System.out);
//System.out.println(System.getProperty("user.dir"));
File file = new File("FileEncodingTest");
File[] files = file.listFiles();
try {
for(File f:files){
tryEncoding(f.getPath());
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void tryEncoding(String fileName) throws IOException {
System.out.println("===Getting encoding of " + fileName);
Path path = Paths.get(fileName);
byte[] data = Files.readAllBytes(path);
CharsetDetector detector = new CharsetDetector();
detector.setText(data);
CharsetMatch match = detector.detect();
String encoding = match.getName();
System.out.println("The Content in " + encoding);
CharsetMatch[] matches = detector.detectAll();
System.out.println("All possibilities:");
for (CharsetMatch m : matches) {
// System.out.println(" CharsetName:" + m.getName() + " Confidence:" + m.getConfidence());
}
}
}
测试发现,记事本保存格式为utf-8的输出格式为utf-8,保存格式为ansi输出为GB18030