java实现 批量转换文件编码格式
转载自https://www.cnblogs.com/enenen/p/10761362.html
在原有基础上增加了原始编码识别,跳过已经是目标编码格式的文件。只需要传文件夹的绝对路径,目标编码格式,需要转换的文件扩展名三个参数就可以了
需要的依赖
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.5.6</version>
</dependency>
<dependency>
<groupId>com.googlecode.juniversalchardet</groupId>
<artifactId>juniversalchardet</artifactId>
<version>1.0.3</version>
</dependency>
public class ConcertEncodeing {
public static void main(String[] args) {
convertCharset("E:\\编程学习\\it-study\\其他\\javaWeb-demo - 副本",StandardCharsets.UTF_8,"java");
}
/**
* 转换文件编码格式
* @param path 需要转换的文件或文件夹路径
* @param toCharset 目标编码格式
* @param expansion 需要转换的文件扩展名,如需全部转换则传 null
*/
private static void convertCharset(String path,Charset toCharset,String expansion ){
if (StrUtil.isBlank(path)) {
return;
}
File file = FileUtil.file(path);
File[] listFiles = file.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
if ((FileUtil.isDirectory(pathname)) || StrUtil.isBlank(expansion) || (!StrUtil.isBlank(expansion) && (FileUtil.extName(pathname).equals("java")))){
return true;
}
return false;
}
});
for (int i = 0; i < listFiles.length; i++) {
if (listFiles[i].isDirectory()) {
String canonicalPath = FileUtil.getCanonicalPath(listFiles[i]);
//每个文件夹分个线程处理,提高点儿效率
new Thread(new Runnable() {
@Override
public void run() {
convertCharset(canonicalPath,toCharset,expansion);
}
}).start();
}else {
try {
Charset fromCharset = Charset.forName(getFileEncode(listFiles[i]));
if(!fromCharset.equals(toCharset)) {
FileUtil.convertCharset(listFiles[i], fromCharset, toCharset);
Console.log("转换完成文件名:{}", listFiles[i].getName());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 获得文件编码格式
*
* @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);
int nread;
while ((nread = in.read(buf)) > 0 && !detector.isDone()) {
detector.handleData(buf, 0, nread);
}
detector.dataEnd();
String encoding = detector.getDetectedCharset();
if (encoding!= null && !encoding.trim().equals("")) {
code = encoding;
}
return code;
}
}