我记得官网文档上说过是不支持gbk格式的,所以只能转码了。
直接上一个转码得工具类
package com.roof.datahandle.fileread;
import java.io.*;
import java.net.URLDecoder;
import java.net.URLEncoder;
/**
* @PackageName:PACKAGE_NAME
* @ClassName:convertFile
* @Description:文件编码转换
* @Author: sunhongchen
* @create 2018-12-14 11:04
*/
public class ConvertFile {
/**
* @param sourceFileRoot 将要转换文件所在的根目录或文件路径
* @param sourceCharset 源文件编码
* @param targetCharset 目标文件编码
* @Author:SunHongchen
* @Description
* @Date: 2018/12/14 11:05
* @Method ConvertFile
* @Return void
*/
public static void convertFile(String sourceFileRoot, String sourceCharset, String targetCharset)
throws IOException {
if (isUTF8File(sourceFileRoot)) {
return;
}
File fileDir = new File(sourceFileRoot);
convert(fileDir, sourceCharset, targetCharset);
}
private static boolean isUTF8File(String filePath) {
boolean isUTF = false;
File f = new File(filePath);
try {
java.io.InputStream ios = new java.io.FileInputStream(f);
byte[] b = new byte[3];
ios.read(b);
ios.close();
if (b[0] == -17 && b[1] == -69 && b[2] == -65) {
System.out.println(f.getName() + "编码为UTF-8");
isUTF = true;
} else {
System.out.println(f.getName() + "可能是GBK");
isUTF = false;
}
} catch (Exception e) {
e.printStackTrace();
}
return isUTF;
}
private static void convert(File file, String sourceCharset, String targetCharset)
throws IOException {
// 如果是文件则进行编码转换,写入覆盖原文件
if (file.isFile()) {
// 只处理.xml结尾的代码文件
if (file.getPath().indexOf(".xml") == -1) {
return;
}
int i = 1;
InputStreamReader isr = new InputStreamReader(new FileInputStream(
file), sourceCharset);
BufferedReader br = new BufferedReader(isr);
//StringBuffer sb = new StringBuffer("<?xml version=\"1.0\" encoding = \""+targetCharset+"\"?>\r\n");
StringBuffer sb = new StringBuffer();
String line = null;
while ((line = br.readLine()) != null) {
if (i == 1) {
if (line.toLowerCase().contains(targetCharset.toLowerCase())) {
br.close();
isr.close();
return;
}
line = line.replaceAll("(?i)" + sourceCharset, targetCharset);
}
// 注意写入换行符
line = URLEncoder.encode(line, "utf8");
sb.append(line + "\r\n");//windows 平台下 换行符为 \r\n
i++;
}
br.close();
isr.close();
File targetFile = new File(file.getPath());
OutputStreamWriter osw = new OutputStreamWriter(
new FileOutputStream(targetFile), targetCharset);
BufferedWriter bw = new BufferedWriter(osw);
// 以字符串的形式一次性写入
bw.write(URLDecoder.decode(sb.toString(), "utf8"));
bw.close();
osw.close();
} else {
//利用递归对目录下的每个以.xml结尾的文件进行编码转换
for (File subFile : file.listFiles()) {
convert(subFile, sourceCharset, targetCharset);
}
}
}
}