java 将GBK编码文件转为UTF-8编码

时间:2021-12-17 14:09:10

需要commons-io-2.0.1.jar

public class Test {
    public static void main(String args[]) throws IOException{
        //GBK编码格式源码路径 
        String srcDirPath = "E:\\UTF8\\shshtv3\\src"; 
        //转为UTF-8编码格式源码路径 
        String utf8DirPath = "E:\\UTF8\\shsht\\src"; 
                
        //获取所有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写入数据 
              FileUtils.writeLines(new File(utf8FilePath), "UTF-8", FileUtils.readLines(javaGbkFile, "GBK"));        
              System.out.println(utf8FilePath);
        }
    }
}