看到一篇关于eclipse导入项目,java文件中文乱码的解决方案,先mark下

转自http://blog.csdn.net/jasonzhou613/article/details/8753628

注:本文来自eclipse导入项目,java文件中文乱码的解决方案

[java]  view plain copy
  1. /** 
  2.  * 建议在转换前先将代码备份 
  3.  *  
  4.  * @date 2012-5-23 
  5.  */  
  6.   
  7. public class ConverEncoding {  
  8.   
  9.     public static void main(String[] args) throws Exception {  
  10.         String srcDir = "F:\\androidproject\\weibo.sdk.android.demo";  
  11.         List<String> files = new ArrayList<String>();  
  12.         fetchFileList(srcDir, files, ".java");  
  13.         for (String fileName : files) {  
  14.             // /  
  15.             convert(fileName, "UTF-8", fileName, "GBK");  
  16.         }  
  17.     }  
  18.   
  19.     /** 
  20.      * 将旧文件的编码更改成另一种编码 并保存到新文件中 
  21.      *  
  22.      * @param oldFile 
  23.      *            旧文件 
  24.      * @param oldCharset 
  25.      *            旧文件的编码 
  26.      * @param newFlie 
  27.      *            新文件 
  28.      * @param newCharset 
  29.      *            新文件的编码 
  30.      */  
  31.   
  32.     public static void convert(String oldFile, String oldCharset,  
  33.             String newFlie, String newCharset) {  
  34.         BufferedReader bin;  
  35.         FileOutputStream fos;  
  36.         StringBuffer content = new StringBuffer();  
  37.         try {  
  38.             System.out.println(oldFile);  
  39.             //   
  40.             bin = new BufferedReader(new InputStreamReader(new FileInputStream(  
  41.                     oldFile), "UTF-8"));  
  42.             String line = null;  
  43.             while ((line = bin.readLine()) != null) {  
  44.                 // System.out.println("content:" + content);  
  45.                 content.append(line);  
  46.                 content.append(System.getProperty("line.separator"));  
  47.             }  
  48.             bin.close();  
  49.             File dir = new File(newFlie.substring(0, newFlie.lastIndexOf("\\")));  
  50.             if (!dir.exists()) {  
  51.                 dir.mkdirs();  
  52.             }  
  53.             fos = new FileOutputStream(newFlie);  
  54.             Writer out = new OutputStreamWriter(fos, newCharset);  
  55.             out.write(content.toString());  
  56.             out.close();  
  57.             fos.close();  
  58.         } catch (UnsupportedEncodingException e) {  
  59.             e.printStackTrace();  
  60.         } catch (FileNotFoundException e) {  
  61.             e.printStackTrace();  
  62.         } catch (IOException e) {  
  63.             e.printStackTrace();  
  64.         }  
  65.     }  
  66.   
  67.     public static void fetchFileList(String strPath, List<String> filelist,  
  68.             final String regex) {  
  69.         File dir = new File(strPath);  
  70.         File[] files = dir.listFiles();  
  71.         Pattern p = Pattern.compile(regex);  
  72.         if (files == null)  
  73.             return;  
  74.         for (int i = 0; i < files.length; i++) {  
  75.             if (files[i].isDirectory()) {  
  76.                 fetchFileList(files[i].getAbsolutePath(), filelist, regex);  
  77.             } else {  
  78.                 String strFileName = files[i].getAbsolutePath().toLowerCase();  
  79.                 Matcher m = p.matcher(strFileName);  
  80.                 if (m.find()) {  
  81.                     filelist.add(strFileName);  
  82.                 }  
  83.             }  
  84.         }  
  85.     }  
  86. }  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值