在一个项目中,如果源码控制得不好,有的同事提交的源码是GBK编码,有的又是utf-8。问题就来了,在Eclipse中打开时很抓狂,无论是在哪种编码方式下,看到的源文件总有些有乱码。在这个问题背景下,试想能否做一个批量转换工具,将某个文件夹下的源文件统一转为某种编码方式呢?
通过查阅相关资料后,发现JDK自带了一个native2ascii转换工具,可以实现单个文件的转换,于是决定动手写一段批量转换的代码。废话少说,见源码:
package com.fayhong.src;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class StreamGobbler extends Thread {
InputStream is;
String type;
StreamGobbler(InputStream is, String type) {
this.is = is;
this.type = type;
}
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null)
System.out.println(type + ">" + line);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
package com.fayhong.src;
import java.io.*;
/**
* 用于文件夹下的java文件的编码自动转换,如gbk转utf-8
* @author Wen Fuqiang
* @company Fayhong Technology Co., Ltd.
* @date 2010-1-26
*/
public class EncodingConverter {
/**
* main方法入口
*
* @param args
* args[0] 给定需要转换的文件夹 args[1] 指定需要转换的编码,如utf-8等
*/
public static void main(String[] args) {
// if (args.length<2){
// System.out.println("please input path of folder and encoding name");
// System.exit(1);
// }
// else{
// ec.convertEncode(args[0], args[1]);
// }
EncodingConverter ec = new EncodingConverter();
//暂时用src_path替换args[0],encoding_name替换arg[1]
String src_path = "E:\\eclipse\\workspace\\BEER\\src\\com\\";
String encoding_name = "utf-8";
ec.convertEncode(src_path, encoding_name);
}
public void convertEncode(String sourceFloder, String encoding_name) {
File file = new File(sourceFloder);
String[] files = file.list();
for (String s : files) {
if (s.indexOf('.') == -1) { // 表明这是个子目录,回归调用此函数
convertEncode(file.getAbsolutePath() + "\\" + s, encoding_name);
} else {
if (s.endsWith("java")) { //只处理以Java结尾的文件
doConvertEncode(file.getAbsolutePath()+ "\\" + s, file.getAbsolutePath()+ "\\" + s, encoding_name);
}
}
}
}
/**
* 完成具体的编码转换工作
* @param inputFile 输入文件
* @param outputFile 输出文件
* @param encoding_name 需要转成的编码格式
*/
public void doConvertEncode(String inputFile, String outputFile, String encoding_name){
Runtime rt = Runtime.getRuntime();
String cmd[] = {"native2ascii.exe","-reverse","-encoding",encoding_name,inputFile,outputFile};
System.out.println("Execing convert command for " + inputFile + " ...");
try {
Process proc = rt.exec(cmd);
// any error message?
StreamGobbler errorGobbler = new
StreamGobbler(proc.getErrorStream(), "ERROR");
// any output?
StreamGobbler outputGobbler = new
StreamGobbler(proc.getInputStream(), "OUTPUT");
// kick them off
errorGobbler.start();
outputGobbler.start();
// any error???
int exitVal = proc.waitFor();
System.out.println("ExitValue: " + exitVal);
} catch (Throwable e) {
e.printStackTrace();
}
}
}