Java批量轉碼 GBK轉爲UTF-8

代碼轉載自:https://www.cnblogs.com/luodengxiong/p/4788862.html

依賴2個jar包需要本時加載:
鏈接:https://pan.baidu.com/s/1HlyBryfNopwXVlAdo8ETfA
提取碼:sj1p

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;


/**
 * 批量將Java源代碼文件的編碼從 GBK 轉爲 UTF-8
 *
 * 運行前需要本地導入 2 個jar包:cpdetector-1.0.10.jar 和 chardet-1.0.jar
 */
public class GBK2UTF8App {

    /**
     * 將制定目錄下的所有Java源文件的編碼格式從GBK修改爲UTF-8
     */
    public static void main(String[] args) throws Exception {
        // GBK編碼格式源碼路徑
        String srcDirPath = "E:\\art\\src";
        // 轉爲UTF-8編碼格式源碼路徑
        String utf8DirPath = "D:\\tmp\\art";
        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));
     * **/

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章