批量将Java源代码文件的编码从GBK转为UTF-8

主要参考:

http://blog.csdn.net/liu_qiqi/article/details/38706497

使用common io批量将java编码从GBK转UTF-8

http://www.oschina.net/code/snippet_97118_11332

Java如何获取文件编码格式

http://www.cnblogs.com/java0721/archive/2012/07/21/2602963.html

把自己修改过的代码贴上来。依赖的包没办法提供了。

package org.xc.binny;

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; public class GBK2UTF8App { /**
* 将制定目录下的所有Java源文件的编码格式从GBK修改为UTF-8
*/
public static void main(String[] args) throws Exception {
// GBK编码格式源码路径
//String srcDirPath = "D:\\workspace-yaoxiao2\\StoreWebWork\\src";
String srcDirPath = "D:\\workspace-yaoxiao2\\StoreEJB\\src";
// 转为UTF-8编码格式源码路径
String utf8DirPath = "D:\\ldx\\utf8\\src2";
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));
* **/ }
上一篇:JavaScript中的Array对象


下一篇:JavaScript中两个对象数组 属性undefined