主要參考:
http://blog.csdn.net/liu_qiqi/article/details/38706497
使用common io批量將java編碼從GBK轉UTF-8
http://www.oschina.net/code/snippet_97118_11332
Java如何獲取文件編碼格式
http://www.cnblogs.com/java0721/archive/2012/07/21/2602963.html
把自己修改過的代碼貼上來。依賴的包沒辦法提供了。
package org.xc.binny; import info.monitorenter.cpdetector.io.ASCIIDetector; import info.monitorenter.cpdetector.io.CodepageDetectorProxy; import info.monitorenter.cpdetector.io.JChardetFacade; import info.monitorenter.cpdetector.io.ParsingDetector; import info.monitorenter.cpdetector.io.UnicodeDetector; import java.io.File; import java.util.Collection; import org.apache.commons.io.FileUtils; public class GBK2UTF8App { /** * 將制定目錄下的所有Java源文件的編碼格式從GBK修改為UTF-8 */ public static void main(String[] args) throws Exception { // GBK編碼格式源碼路徑 //String srcDirPath = "D:\\workspace-yaoxiao2\\StoreWebWork\\src"; String srcDirPath = "D:\\workspace-yaoxiao2\\StoreEJB\\src"; // 轉為UTF-8編碼格式源碼路徑 String utf8DirPath = "D:\\ldx\\utf8\\src2"; String charsetNameUtf8="UTF-8"; // 獲取所有java文件 Collection<File> javaGbkFileCol = FileUtils.listFiles(new File(srcDirPath), new String[] { "java" }, true); for (File javaGbkFile : javaGbkFileCol) { // UTF8格式文件路徑 String utf8FilePath2 = utf8DirPath + javaGbkFile.getAbsolutePath().substring(srcDirPath.length()); String srcDirPath2 = srcDirPath + javaGbkFile.getAbsolutePath().substring(srcDirPath.length()); // 使用GBK讀取數據,然后用UTF-8寫入數據 String charsetName = getFileEncode(srcDirPath2); if(!charsetNameUtf8.equals(charsetName)){ System.out.println(javaGbkFile.getName() +":"+ charsetName); FileUtils.writeLines(new File(utf8FilePath2), "UTF-8", FileUtils.readLines(javaGbkFile, charsetName)); } } } /** * 利用第三方開源包cpdetector獲取文件編碼格式 * * @param path * 要判斷文件編碼格式的源文件的路徑 * @author huanglei * @version 2012-7-12 14:05 */ public static String getFileEncode(String path) { /* * detector是探測器,它把探測任務交給具體的探測實現類的實例完成。 * cpDetector內置了一些常用的探測實現類,這些探測實現類的實例可以通過add方法 加進來,如ParsingDetector、 * JChardetFacade、ASCIIDetector、UnicodeDetector。 * detector按照“誰最先返回非空的探測結果,就以該結果為准”的原則返回探測到的 * 字符集編碼。使用需要用到三個第三方JAR包:antlr.jar、chardet.jar和cpdetector.jar * cpDetector是基於統計學原理的,不保證完全正確。 */ CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance(); /* * ParsingDetector可用於檢查HTML、XML等文件或字符流的編碼,構造方法中的參數用於 * 指示是否顯示探測過程的詳細信息,為false不顯示。 */ detector.add(new ParsingDetector(false)); /* * JChardetFacade封裝了由Mozilla組織提供的JChardet,它可以完成大多數文件的編碼 * 測定。所以,一般有了這個探測器就可滿足大多數項目的要求,如果你還不放心,可以 * 再多加幾個探測器,比如下面的ASCIIDetector、UnicodeDetector等。 */ detector.add(JChardetFacade.getInstance());// 用到antlr.jar、chardet.jar // ASCIIDetector用於ASCII編碼測定 detector.add(ASCIIDetector.getInstance()); // UnicodeDetector用於Unicode家族編碼的測定 detector.add(UnicodeDetector.getInstance()); java.nio.charset.Charset charset = null; File f = new File(path); try { charset = detector.detectCodepage(f.toURI().toURL()); } catch (Exception ex) { ex.printStackTrace(); } if (charset != null) return charset.name(); else return null; } /** * URL url = CreateStationTreeModel.class.getResource("/resource/" + * "配置文件"); URLConnection urlConnection = url.openConnection(); * inputStream=urlConnection.getInputStream(); String charsetName = * getFileEncode(url); System.out.println(charsetName); BufferedReader in = * new BufferedReader(new InputStreamReader(inputStream, charsetName)); * **/ }