EncodeConvert編碼轉換工具
阿新 • • 發佈:2019-02-17
用法:
EncodeConvert.jar "args1" "args2" "args3"
引數:
args1 原始碼的根目錄
args2 轉換後的檔案目錄
args3 "g2u":GBK轉UTF-8 "u2g":UTF-8轉GBK
例子:
GBK轉UTF-8--->EncodeConvert.jar "E:\GBK\src" "E:\UTF8\src1" "g2u"
UTF-8轉GBK--->EncodeConvert.jar "E:\UTF8\src" "E:\GBK\src1" "u2g"
2012-09-28 11:10:18 by Yong
import java.io.IOException; public class Main { /** * @param args */ public static void main(String[] args) { if (args[2].equals("u2g")) { try { Convert.UTF8toGBK(args[0], args[1]); } catch (IOException e) { e.printStackTrace(); } } else if (args[2].equals("g2u")) { try { Convert.GBKtoUTF8(args[0], args[1]); } catch (IOException e) { e.printStackTrace(); } } } }
import java.io.File; import java.io.IOException; import java.util.Collection; import java.util.Iterator; import org.apache.commons.io.FileUtils; public class Convert { /** * 將制定目錄下的所有Java原始檔的編碼格式從GBK修改為UTF-8 */ public static final String[] javastr = { "java" }; public static void GBKtoUTF8(String GBKsrc, String toUTF8src) throws IOException { // GBK編碼格式原始碼路徑 String srcDirPath = GBKsrc; // 轉為UTF-8編碼格式原始碼路徑 String utf8DirPath = toUTF8src; // 獲取所有java檔案 Collection<File> javaGbkFileCol = FileUtils.listFiles(new File(srcDirPath), javastr, true); for (Iterator<File> iterator = javaGbkFileCol.iterator(); iterator.hasNext();) { File javaGbkFile = iterator.next(); // UTF8格式檔案路徑 String utf8FilePath = utf8DirPath + javaGbkFile.getAbsolutePath().substring(srcDirPath.length()); // 使用GBK讀取資料,然後用UTF-8寫入資料 FileUtils.writeLines(new File(utf8FilePath), "UTF-8", FileUtils.readLines(javaGbkFile, "GBK")); System.out.println("轉換完成!"); } } /** * 將制定目錄下的所有Java原始檔的編碼格式從UTF-8修改為GBK */ public static void UTF8toGBK(String UTF8src, String toGBKsrc) throws IOException { // UTF-8編碼格式原始碼路徑 String srcDirPath = UTF8src; // 轉為GBK編碼格式原始碼路徑 String gbkDirPath = toGBKsrc; // 獲取所有java檔案 Collection<File> javaUtf8FileCol = FileUtils.listFiles(new File(srcDirPath), javastr, true); for (Iterator<File> iterator = javaUtf8FileCol.iterator(); iterator.hasNext();) { File javaUtf8File = iterator.next(); // UTF8格式檔案路徑 String GBK8FilePath = gbkDirPath + javaUtf8File.getAbsolutePath().substring(srcDirPath.length()); // 使用GBK讀取資料,然後用UTF-8寫入資料 FileUtils.writeLines(new File(GBK8FilePath), "GBK", FileUtils.readLines(javaUtf8File, "UTF-8")); System.out.println("轉換完成!"); } } }