java批量将GBK项目源码转成utf-8

今天从git上下载的一个项目是GBK编码的,然后本地项目一般是utf-8编码的,于是写了一小段程序将GBK项目转成utf-8项目。

需要apache 的commons-io-2.5.jar

代码如下:

package com.usench;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Properties;

import org.apache.commons.io.FileUtils;

/**
 * @ClassName: GbkFileToUtf8Batch
 * @Description: 批量将GBK文件转存为utf8编码保存
 * @date 2016年5月19日 下午5:24:48
 * @author wmj911
 *
 */
public class GbkFileToUtf8Batch {

    public static void main(String[] args) throws IOException {
        Properties prop = new Properties();
        prop.load(GbkFileToUtf8Batch.class.getClassLoader().getResourceAsStream("param.properties"));
       
        //GBK编码格式源码路径
        String srcDirPath = prop.getProperty("gbkDirPath");
        //转为UTF-8编码格式源码路径
        String utf8DirPath = prop.getProperty("utf8DirPath");
       
        System.out.println("srcDirPath="+srcDirPath+",utf8DirPath="+utf8DirPath);
               
        //获取所有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("批量编码完成.....");
    }
}

猜你喜欢

转载自usench.iteye.com/blog/2299959