Java源码乱码问题 编码转换器

来源:互联网 发布:杀蚊软件 编辑:程序博客网 时间:2024/06/07 07:27
注释:开发背景:导入到myeclipse中的源码是gbk编码,项目是utf-8编码,导致查看源码乱码问题。

package com.test;

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

import org.apache.commons.io.FileUtils;
import org.junit.Test;
/**

* @FileName    : Transcode
* @Description : 文件编码转换器
* @Copyright   : PowerData Software Co.,Ltd.Rights Reserved 
* @Company     : 深圳市博安达软件开发有限公司
* @author      : 陈海新
* @version     : 1.0
* Create Date  : 2014-11-12 上午11:47:10
*/
public class Transcode {
        /**
          * @Title: transcode 
          * @Description: 源代码乱码转换器 
          * @param sourcePath 源代码路径
          * @param oldCode 源代码原来的编码格式
          * @param newCode 源代码需要转换成的编码格式
          * @param extensions an array of extensions, ex. {"java","xml"}. If this parameter is null, all files are returned   
          * @return:void
         */
        public void transcode(String sourcePath,String oldCode,String newCode,String[] extensions){
                Collection<File> javaGbkFileCol =  FileUtils.listFiles(new File(sourcePath), extensions, true);
                for (File gbkFile : javaGbkFileCol) { 
                      try {
                                FileUtils.writeLines(new File(gbkFile.getAbsolutePath()), newCode, FileUtils.readLines(gbkFile,oldCode));
                        } catch (IOException e) {
                                e.printStackTrace();
                        }        
                 }
        }
        @Test
        public void test(){
                new Transcode().transcode("C:/Users/chx/Desktop/alisoft-xplatform-asf-cache-2.5.1-src资料/alisoft-xplatform-asf-cache-2.5.1-src资料/alisoft-xplatform-asf-cache-2.5.1-src","GBK","UTF-8",null);
        }
}
0 0
原创粉丝点击