码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2014-11-13 00:20:18      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:des   style   io   ar   java   sp   for   文件   div   

注释:开发背景:导入到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   : XXXX
 * @Company     : XXXXXX
 * @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);
 }
}

 

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

标签:des   style   io   ar   java   sp   for   文件   div   

原文地址:http://www.cnblogs.com/yuyanbian/p/4093828.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!