1. 程式人生 > >Java專案編碼GBK轉換UTF8

Java專案編碼GBK轉換UTF8

依賴jar

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.5</version>
        </dependency>
public static void main(String[] args) {
        //GBK編碼格式原始碼路徑
        String srcDirPath = "D:\\Users\\IdeaWorkSpace\\gitee"
; //轉為UTF-8編碼格式原始碼路徑 String utf8DirPath = "D:\\Users\\IdeaWorkSpace\\gitee\\UTF8"; //獲取所有java檔案 Collection<File> javaGbkFileCol = FileUtils.listFiles(new File(srcDirPath), new String[]{"java"}, true); for (File javaGbkFile : javaGbkFileCol) { //UTF8格式檔案路徑
String utf8FilePath = utf8DirPath+javaGbkFile.getAbsolutePath().substring(srcDirPath.length()); //使用GBK讀取資料,然後用UTF-8寫入資料 try { FileUtils.writeLines(new File(utf8FilePath), "UTF-8", FileUtils.readLines(javaGbkFile, "GBK")); } catch (IOException e) { e.printStackTrace(); } } }