由於第三方給出的csv編碼格式問題導致無法導入指定的庫表,網上找了許多轉換的插件總是不滿意。折騰了大半天想想還是自己改造下吧。該工具包解決了幾乎所有的編碼問題,若是不足之處請指出。由於文件暫存在String類型裏面,要是文件太大的話會報內存溢出的錯誤可以自行調整下代碼。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
public class FileCharsetConverter {
public static void main(String[] args) throws Exception {
String dirPath ="C:\\Users\\oftoo\\Desktop\\服務默認\\原編碼\\xx\\xx";
File dirFile = new File(dirPath);
File[] files = dirFile.listFiles();
if (null == files) {
System.out.println("路徑:" + dirPath + ",該文件夾下沒有文件");
} else {
for (int i = 0; i < files.length; i++) {
File file = files[i];
String destFileName = file.getCanonicalPath();
File destFile = new File(destFileName);
if (destFile.exists()) {
convert(destFile.getCanonicalPath(),
"UTF-16LE", "GBK", new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith("csv");
}
});
} else {
System.out.println("路徑:" + dirPath + ",該文件夾下沒有文件");
}
}
System.out.println("編碼轉換結束");
}
/*convert("C:\\Users\\xieyh\\Desktop\\測試編碼\\LRL_HairStore_Tmall_Acxiom_Transaction_20170619.csv",
"UTF-16LE", "GBK", new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith("csv");
}
}); */
}
public static String getParentPath(File file) {
if (null != file && file.exists()) {
return file.getParent();
}
return "";
}
/**
* 把指定文件或目錄轉換成指定的編碼
*
* @param fileName
* 要轉換的文件
* @param fromCharsetName
* 源文件的編碼
* @param toCharsetName
* 要轉換的編碼
* @throws Exception
*/
public static void convert(String fileName, String fromCharsetName,
String toCharsetName) throws Exception {
convert(new File(fileName), fromCharsetName, toCharsetName, null);
}
/**
* 把指定文件或目錄轉換成指定的編碼
*
* @param file
* 要轉換的文件或目錄
* @param fromCharsetName
* 源文件的編碼
* @param toCharsetName
* 要轉換的編碼
* @throws Exception
*/
public static void convert(File file, String fromCharsetName,
String toCharsetName) throws Exception {
convert(file, fromCharsetName, toCharsetName, null);
}
/**
* 把指定文件或目錄轉換成指定的編碼
*
* @param file
* 要轉換的文件或目錄
* @param fromCharsetName
* 源文件的編碼
* @param toCharsetName
* 要轉換的編碼
* @param filter
* 文件名過濾器
* @throws Exception
*/
public static void convert(String fileName, String fromCharsetName,
String toCharsetName, FilenameFilter filter) throws Exception {
convert(new File(fileName), fromCharsetName, toCharsetName, filter);
}
/**
* 把指定文件或目錄轉換成指定的編碼
*
* @param file
* 要轉換的文件或目錄
* @param fromCharsetName
* 源文件的編碼
* @param toCharsetName
* 要轉換的編碼
* @param filter
* 文件名過濾器
* @throws Exception
*/
public static void convert(File file, String fromCharsetName,
String toCharsetName, FilenameFilter filter) throws Exception {
if (file.isDirectory()) {
File[] fileList = null;
if (filter == null) {
fileList = file.listFiles();
} else {
fileList = file.listFiles(filter);
}
for (File f : fileList) {
convert(f, fromCharsetName, toCharsetName, filter);
}
} else {
if (filter == null
|| filter.accept(file.getParentFile(), file.getName())) {
String fileContent = getFileContentFromCharset(file,
fromCharsetName);
saveFile2Charset(file, toCharsetName, fileContent);
}
}
}
/**
* 以指定編碼方式讀取文件,返回文件內容
*
* @param file
* 要轉換的文件
* @param fromCharsetName
* 源文件的編碼
* @return
* @throws Exception
*/
public static String getFileContentFromCharset(File file,
String fromCharsetName) throws Exception {
if (!Charset.isSupported(fromCharsetName)) {
throw new UnsupportedCharsetException(fromCharsetName);
}
InputStream inputStream = new FileInputStream(file);
InputStreamReader reader = new InputStreamReader(inputStream,
fromCharsetName);
char[] chs = new char[(int) file.length()];
reader.read(chs);
String str = new String(chs).trim();
reader.close();
return str;
}
/**
* 以指定編碼方式寫文本文件,存在會覆蓋
*
* @param file
* 要寫入的文件
* @param toCharsetName
* 要轉換的編碼
* @param content
* 文件內容
* @throws Exception
*/
public static void saveFile2Charset(File file, String toCharsetName,
String content) throws Exception {
if (!Charset.isSupported(toCharsetName)) {
throw new UnsupportedCharsetException(toCharsetName);
}
OutputStream outputStream = new FileOutputStream(file);
OutputStreamWriter outWrite = new OutputStreamWriter(outputStream,
toCharsetName);
outWrite.write(content);
outWrite.close();
}
}