eclipse导入项目,java文件中文乱码的解决方案

都在看别人的一些代码,其中中苦恼的是将别人写的工程导入Eclipse里的时候,java文件出现了乱码。因为工程的原始编码是GBK的,但是我一般使用的是UTF-8的编码,结果就会出现编码不一致,出现代码乱码的情况。

也试过几种方法比如在eclipse中修改文件的编码,虽然在显示时是解决了乱码的问题,但是在运行时就会出现乱码情况,我新建的文件是UTF-8,原来导入的又是GBK,所以就会出现乱码。

最笨的方法就是将GBK的文件用记事本打开,一个一个copy到eclipse里,这个方法是可行的,可以解决编码问题,但是一但文件多起来,这个方法就是一件很累人的事情了。。。。

一气之下决定写个程序来实现自动转换编码的方法,其实想想也是很简单的,只不过是让机器帮我们实现记事本打开然后copy到别一个UTF-8文件的过程。

废话不多说,直接上代码吧:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package com.buptsse.ate.utils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
* @author linzhe
* @email  m23linzhe@gmail.com
* @date   2012-5-23
*/
public class ConverEncoding {
   /**
    *
    * @param args
    * @throws Exception
    */
   public static void main(String[] args) throws Exception {
      String srcDir = "E:\\mining\\tools\\ictclas4j\\ictclas4j\\src" ;
      List<String> files = new ArrayList<String>();
      fetchFileList(srcDir, files, ".java" );
      for (String fileName : files){
         convert(fileName, "GBK" , fileName, "UTF-8" );
      }
   }
   public static void convert(String oldFile, String oldCharset, String newFlie, String newCharset){
      BufferedReader bin;
      FileOutputStream fos;
      StringBuffer content = new StringBuffer();
      try {
         System.out.println(oldFile);
         bin = new BufferedReader( new InputStreamReader( new FileInputStream(oldFile), "gbk" ));
         String line = null ;
         while ((line=bin.readLine())!= null ){
//          System.out.println("content:" + content);
            content.append(line);
            content.append(System.getProperty( "line.separator" ));
         }
         bin.close();
         File dir = new File(newFlie.substring( 0 , newFlie.lastIndexOf( "\\" )));
         if (!dir.exists()){
            dir.mkdirs();
         }
         fos = new FileOutputStream(newFlie);
         Writer out = new OutputStreamWriter(fos, newCharset);
         out.write(content.toString());
         out.close();
         fos.close();
      } catch (UnsupportedEncodingException e) {
         e.printStackTrace();
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
   public static void fetchFileList(String strPath, List<String> filelist, final String regex) {
      File dir = new File(strPath);
        File[] files = dir.listFiles();
        Pattern p = Pattern.compile(regex);
        if (files == null )
            return ;
        for ( int i = 0 ; i < files.length; i++) {
            if (files[i].isDirectory()) {
               fetchFileList(files[i].getAbsolutePath(), filelist, regex);
            } else {
                String strFileName = files[i].getAbsolutePath().toLowerCase();
                Matcher m = p.matcher(strFileName);
                if (m.find()){
                  filelist.add(strFileName);
                }
            }
        }
    }
}

  • 6
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 16
    评论
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值