from : http://blog.csdn.net/defonds/article/details/4925169
测试使用说明:
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 下文件编码格式进行批量转换源代码如下:
- package demo;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileFilter;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.io.PrintWriter;
- import java.io.UnsupportedEncodingException;
- /**
- * 项目名称:Test64.0-ec
- * 类名称:EncodeConverter
- * 类描述:Linux 下批量转换文件编码格式
- * 创建人:Defonds
- * 创建时间:2009-12-2 下午03:37:15
- * 修改人:Defonds
- * 修改时间:2009-12-2 下午03:37:15
- */
- public class EncodeConverter
- {
- // 原文件目录
- private static String srcDir = "//home//defonds//tmp//test//";
- // 转换后的存放目录
- private static String desDir = "//home//defonds//tmp//test2//";
- // 源文件编码
- private static String srcEncode = "gb2312";
- // 输出文件编码
- private static String desEncode = "utf-8";
- /**
- * 处理的文件过滤,过滤器
- */
- private static FileFilter filter = new FileFilter()
- {
- public boolean accept(File pathname)
- {
- // 只处理:目录 或是 .java文件
- if(pathname.isDirectory() || (pathname.isFile() && pathname.getName().endsWith(".java")))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- };
- /**
- * @param file
- */
- public static void readDir(File file)
- {
- // 以过滤器作为参数
- File[] files = file.listFiles(filter);
- for (File subFile : files)
- {
- /**
- * 建立目标目录
- */
- if (subFile.isDirectory())
- {
- File file3 = new File(desDir + subFile.getAbsolutePath().substring(srcDir.length()));
- if (!file3.exists())
- {
- file3.mkdir();
- }
- file3 = null;
- readDir(subFile);
- }
- /**
- * 建立目标文件
- */
- else
- {
- System.err.println("一源文件:/t" + subFile.getAbsolutePath() + "/n目标文件:/t" + (desDir + subFile.getAbsolutePath().substring(srcDir.length() - 5)));
- System.err.println("-----------------------------------------------------------------");
- try
- {
- convert(subFile.getAbsolutePath(),desDir + subFile.getAbsolutePath().substring(srcDir.length() - 5),srcEncode, desEncode);
- } catch (UnsupportedEncodingException e)
- {
- e.printStackTrace();
- } catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- }
- }
- /**
- * @param infile 源文件路径
- * @param outfile 输出文件路径
- * @param from 源文件编码
- * @param to 目标文件编码
- * @throws IOException
- * @throws UnsupportedEncodingException
- */
- public static void convert(String infile, String outfile, String from,
- String to) throws IOException,UnsupportedEncodingException
- {
- BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(infile), from));
- PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outfile), to)));
- String reading;
- while ((reading = in.readLine()) != null)
- {
- out.println(reading);
- }
- out.close();
- in.close();
- }
- public static void main(String[] args)
- {
- // 建立目标文件夹
- File desFile = new File(desDir);
- if (!desFile.exists())
- {
- desFile.mkdir();
- }
- desFile = null;
- File srcFile = new File(srcDir);
- // 读取目录 循环转换文件
- readDir(srcFile);
- srcFile = null;
- }
- }