使用 Java 对 Linux 下文件编码格式进行批量转换


        源码下载

        测试使用说明:
        1。将 字符集编码格式为 GB2312 的文件 test0.java,test1.java,test2.java 放在 /home/defonds/tmp/test 目录下(test0.java,test1.java,test2.java 作者可以自行随意创建。如何查看 test0.java 的字符集编码格式?打开 test0.java ,文件 -> 另存为,弹出的对话框里的“字符集编码”即当前文件的字符集编码格式。)。
        2。新建一目录 /home/defonds/tmp/test2(也可以不建,程序可以自动生成)。运行 EncodeConverter.java。
        3。打开 /home/defonds/tmp/test2 文件夹,发现有新文件 test0.java,test1.java,test2.java 生成,查看其编码格式,是为 UTF-8,证明转码成功。
        4。使用本工具可以对 Linux 下文件的字符集编码格式进行批量转换。只需适当配置一下,可以批量处理各种文件格式的文件,如 *.txt,*.java 等等。

        使用 Java 对 Linux 下文件编码格式进行批量转换源代码如下:

[java]  view plain copy print ?
  1. package demo;  
  2. import java.io.BufferedReader;  
  3. import java.io.BufferedWriter;  
  4. import java.io.File;  
  5. import java.io.FileFilter;  
  6. import java.io.FileInputStream;  
  7. import java.io.FileOutputStream;  
  8. import java.io.IOException;  
  9. import java.io.InputStreamReader;  
  10. import java.io.OutputStreamWriter;  
  11. import java.io.PrintWriter;  
  12. import java.io.UnsupportedEncodingException;  
  13. /** 
  14.  * 项目名称:Test64.0-ec 
  15.  * 类名称:EncodeConverter 
  16.  * 类描述:Linux 下批量转换文件编码格式 
  17.  * 创建人:Defonds 
  18.  * 创建时间:2009-12-2 下午03:37:15 
  19.  * 修改人:Defonds 
  20.  * 修改时间:2009-12-2 下午03:37:15 
  21.  */  
  22. public class EncodeConverter  
  23. {  
  24.     // 原文件目录  
  25.     private static String srcDir = "//home//defonds//tmp//test//";  
  26.     // 转换后的存放目录  
  27.     private static String desDir = "//home//defonds//tmp//test2//";  
  28.     // 源文件编码  
  29.     private static String srcEncode = "gb2312";  
  30.     // 输出文件编码  
  31.     private static String desEncode = "utf-8";  
  32.     /** 
  33.      * 处理的文件过滤,过滤器 
  34.      */  
  35.     private static FileFilter filter = new FileFilter()  
  36.     {  
  37.         public boolean accept(File pathname)  
  38.         {  
  39.             // 只处理:目录 或是 .java文件  
  40.             if(pathname.isDirectory() || (pathname.isFile() && pathname.getName().endsWith(".java")))  
  41.             {  
  42.                 return true;  
  43.             }  
  44.             else  
  45.             {  
  46.                 return false;  
  47.             }  
  48.         }  
  49.     };  
  50.     /** 
  51.      * @param file 
  52.      */  
  53.     public static void readDir(File file)  
  54.     {  
  55.         // 以过滤器作为参数  
  56.         File[] files = file.listFiles(filter);  
  57.         for (File subFile : files)  
  58.         {  
  59.             /** 
  60.              * 建立目标目录 
  61.              */  
  62.             if (subFile.isDirectory())  
  63.             {  
  64.                 File file3 = new File(desDir + subFile.getAbsolutePath().substring(srcDir.length()));  
  65.                 if (!file3.exists())  
  66.                 {  
  67.                     file3.mkdir();  
  68.                 }  
  69.                 file3 = null;  
  70.                 readDir(subFile);  
  71.             }   
  72.             /** 
  73.              * 建立目标文件 
  74.              */  
  75.             else  
  76.             {  
  77.                 System.err.println("一源文件:/t" + subFile.getAbsolutePath() + "/n目标文件:/t" + (desDir + subFile.getAbsolutePath().substring(srcDir.length() - 5)));  
  78.                 System.err.println("-----------------------------------------------------------------");  
  79.                 try  
  80.                 {  
  81.                     convert(subFile.getAbsolutePath(),desDir + subFile.getAbsolutePath().substring(srcDir.length() - 5),srcEncode, desEncode);  
  82.                 } catch (UnsupportedEncodingException e)  
  83.                 {  
  84.                     e.printStackTrace();  
  85.                 } catch (IOException e)  
  86.                 {  
  87.                     e.printStackTrace();  
  88.                 }  
  89.             }  
  90.         }  
  91.     }  
  92.     /** 
  93.      * @param infile 源文件路径 
  94.      * @param outfile 输出文件路径 
  95.      * @param from 源文件编码 
  96.      * @param to 目标文件编码 
  97.      * @throws IOException 
  98.      * @throws UnsupportedEncodingException 
  99.      */  
  100.     public static void convert(String infile, String outfile, String from,  
  101.             String to) throws IOException,UnsupportedEncodingException  
  102.     {  
  103.         BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(infile), from));  
  104.         PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outfile), to)));  
  105.         String reading;  
  106.         while ((reading = in.readLine()) != null)  
  107.         {  
  108.             out.println(reading);  
  109.         }  
  110.         out.close();  
  111.         in.close();  
  112.     }  
  113.     public static void main(String[] args)  
  114.     {  
  115.         // 建立目标文件夹  
  116.         File desFile = new File(desDir);  
  117.         if (!desFile.exists())  
  118.         {  
  119.             desFile.mkdir();  
  120.         }  
  121.         desFile = null;  
  122.         File srcFile = new File(srcDir);  
  123.         // 读取目录 循环转换文件  
  124.         readDir(srcFile);  
  125.         srcFile = null;  
  126.     }  
  127. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值