Java代码:
package utils;
import org.mozilla.intl.chardet.nsDetector;
import org.mozilla.intl.chardet.nsICharsetDetectionObserver;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class Gbk2utf8 {
public static void gbk2Utf8(String fileName) {
BufferedReader reader = null;
BufferedWriter writer = null;
try {
StringBuffer sb = new StringBuffer();
reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "GBK"));
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName+"tmp"), "UTF-8"));
String str;
while ((str = reader.readLine()) != null) {
sb.append(str).append("\r\n");
writer.write(sb.toString());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 递归获取指定目录下所有指定类型文件
*
* @param strPath
* 文件夹地址
* @param suffix
* 文件名后缀
* @return
*/
public static List<File> getFileList(String strPath, String suffix) {
List<File> filelist = new ArrayList<File>();
getFileList(strPath, suffix, filelist);
return filelist;
}
public static void getFileList(String strPath, String suffix,List<File> fileList) {
File dir = new File(strPath);
File[] files = dir.listFiles(); // 该文件目录下文件全部放入数组
if (files != null) {
for (int i = 0; i < files.length; i++) {
String fileName = files[i].getName();
if (files[i].isDirectory()) { // 如果是文件夹就递归调用
getFileList(files[i].getAbsolutePath(), suffix,fileList);
} else if (fileName.endsWith(suffix)) {
fileList.add(files[i]);
}
}
}
}
// 是否找到匹配字符集
private static boolean isFind = false;
// 如果完全匹配某个字符集检测算法, 则该属性保存该字符集的名称. 否则(如二进制文件)其值就为默认值 null
private static String encoding = null;
/**
* 获取文件的编码
*
* @param file
* @return 文件编码,若无,则返回null
* @throws IOException
*/
private static String guessFileCharset(File file) throws IOException {
nsDetector det = new nsDetector();
det.Init(new nsICharsetDetectionObserver() {
public void Notify(String charset) {
isFind = true;
encoding = charset;
}
});
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
byte[] buf = new byte[1024];
int len;
boolean done = false;
boolean isAscii = true;
while ((len = bis.read(buf, 0, buf.length)) != -1) {
if (isAscii) {
isAscii = det.isAscii(buf, len);
} else if (!done) {
done = det.DoIt(buf, len, false);
}
}
det.DataEnd();
if (isAscii) {
encoding = "ASCII";
isFind = true;
} else if (!isFind) {
String prob[] = det.getProbableCharsets();
if (prob.length > 0) {
encoding = prob[0]; // 在没有发现情况下,则取第一个可能的编码
}
}
return encoding;
}
public static void main(String[] args) {
List<File> files = getFileList("F:\\ppppppppp", ".sql");
for (int i = 0; i < files.size(); i++) {
String charset = null;
try {
System.out.print("正在获取第"+i+"个文件的编码"+"==="+files.get(i).getAbsoluteFile());
charset = guessFileCharset(files.get(i).getAbsoluteFile());
} catch (IOException e) {
System.err.println("获取文件编码发生异常!");
}
System.out.println("====="+files.get(i).getAbsoluteFile() + "[" + charset + "]");
if ("GB2312".equals(charset)) {
System.out.println("正在获取第"+i+"个文件的转化"+"==="+files.get(i).getAbsoluteFile());
gbk2Utf8(files.get(i).getAbsolutePath());
files.get(i).delete();
}
}
System.out.println("文件总数:"+files.size());
}
}
pom文件:
<dependency>
<groupId>net.sourceforge.jchardet</groupId>
<artifactId>jchardet</artifactId>
<version>1.0</version>
</dependency>