java在读取文件时容易出现乱码,究其原因是读取文件的编码跟文件本身的编码不一致,那在解析文件前如何获取文件的编码格式呢?本文主要通过google开源的 juniversalchardet 来实现。
首先在项目中引入jar包
<dependency>
<groupId>com.googlecode.juniversalchardet</groupId>
<artifactId>juniversalchardet</artifactId>
<version>1.0.3</version>
</dependency>
代码封装如下:
/**
* 获得文件编码格式
*
* @param file
* @return
* @throws IOException
*/
public static String getFileEncode(File file) throws IOException {
FileInputStream in = new FileInputStream(file);
String code = "utf-8";
byte[] buf = new byte[4096];
UniversalDetector detector = new UniversalDetector(null);
// (2)
int nread;
while ((nread = in.read(buf)) > 0 && !detector.isDone()) {
detector.handleData(buf, 0, nread);
}
// (3)
detector.dataEnd();
// (4)
String encoding = detector.getDetectedCharset();
if (StringUtils.isNotEmpty(encoding)) {
code = encoding;
}
return code;
}
调用该方法就会返回文件的编码格式,然后通过指定编码读取文件就不会乱码啦