使用java进行文件编码转换

  1. 在开发过程中,可能会遇到文件编码的转换,虽然说开发工具eclipse可以转换编码,但是有的情况却很不方便。比如,原来文件本身的编码是GBK,现在要转换成UTF-8,如果直接在eclipse中把文件编码修改成UTF-8,恭喜你,是乱码,因为不能直接从GBK到UTF-8进行转换,这时就需要我们手动的来转换编码。下面是一个文件编码转换的工具类。

  2. package com.mikan.stuff;  
  3.   
  4. import java.io.File;  
  5. import java.io.FileInputStream;  
  6. import java.io.FileOutputStream;  
  7. import java.io.FilenameFilter;  
  8. import java.io.InputStream;  
  9. import java.io.InputStreamReader;  
  10. import java.io.OutputStream;  
  11. import java.io.OutputStreamWriter;  
  12. import java.nio.charset.Charset;  
  13. import java.nio.charset.UnsupportedCharsetException;  
  14.   
  15. public class FileCharsetConverter {  
  16.   
  17.     public static void main(String[] args) throws Exception {  
  18.         convert("D:\\stuff\\src\\main\\java\\com\\mikan\\stuff\\test.txt",  
  19.                 "GBK""UTF-8"new FilenameFilter() {  
  20.                     @Override  
  21.                     public boolean accept(File dir, String name) {  
  22.                         return name.endsWith("txt");  
  23.                     }  
  24.                 });  
  25.     }  
  26.   
  27.     /** 
  28.      * 把指定文件或目录转换成指定的编码 
  29.      *  
  30.      * @param fileName 
  31.      *            要转换的文件 
  32.      * @param fromCharsetName 
  33.      *            源文件的编码 
  34.      * @param toCharsetName 
  35.      *            要转换的编码 
  36.      * @throws Exception 
  37.      */  
  38.     public static void convert(String fileName, String fromCharsetName,  
  39.             String toCharsetName) throws Exception {  
  40.         convert(new File(fileName), fromCharsetName, toCharsetName, null);  
  41.     }  
  42.   
  43.     /** 
  44.      * 把指定文件或目录转换成指定的编码 
  45.      *  
  46.      * @param file 
  47.      *            要转换的文件或目录 
  48.      * @param fromCharsetName 
  49.      *            源文件的编码 
  50.      * @param toCharsetName 
  51.      *            要转换的编码 
  52.      * @throws Exception 
  53.      */  
  54.     public static void convert(File file, String fromCharsetName,  
  55.             String toCharsetName) throws Exception {  
  56.         convert(file, fromCharsetName, toCharsetName, null);  
  57.     }  
  58.   
  59.     /** 
  60.      * 把指定文件或目录转换成指定的编码 
  61.      *  
  62.      * @param file 
  63.      *            要转换的文件或目录 
  64.      * @param fromCharsetName 
  65.      *            源文件的编码 
  66.      * @param toCharsetName 
  67.      *            要转换的编码 
  68.      * @param filter 
  69.      *            文件名过滤器 
  70.      * @throws Exception 
  71.      */  
  72.     public static void convert(String fileName, String fromCharsetName,  
  73.             String toCharsetName, FilenameFilter filter) throws Exception {  
  74.         convert(new File(fileName), fromCharsetName, toCharsetName, filter);  
  75.     }  
  76.   
  77.     /** 
  78.      * 把指定文件或目录转换成指定的编码 
  79.      *  
  80.      * @param file 
  81.      *            要转换的文件或目录 
  82.      * @param fromCharsetName 
  83.      *            源文件的编码 
  84.      * @param toCharsetName 
  85.      *            要转换的编码 
  86.      * @param filter 
  87.      *            文件名过滤器 
  88.      * @throws Exception 
  89.      */  
  90.     public static void convert(File file, String fromCharsetName,  
  91.             String toCharsetName, FilenameFilter filter) throws Exception {  
  92.         if (file.isDirectory()) {  
  93.             File[] fileList = null;  
  94.             if (filter == null) {  
  95.                 fileList = file.listFiles();  
  96.             } else {  
  97.                 fileList = file.listFiles(filter);  
  98.             }  
  99.             for (File f : fileList) {  
  100.                 convert(f, fromCharsetName, toCharsetName, filter);  
  101.             }  
  102.         } else {  
  103.             if (filter == null  
  104.                     || filter.accept(file.getParentFile(), file.getName())) {  
  105.                 String fileContent = getFileContentFromCharset(file,  
  106.                         fromCharsetName);  
  107.                 saveFile2Charset(file, toCharsetName, fileContent);  
  108.             }  
  109.         }  
  110.     }  
  111.   
  112.     /** 
  113.      * 以指定编码方式读取文件,返回文件内容 
  114.      * 
  115.      * @param file 
  116.      *            要转换的文件 
  117.      * @param fromCharsetName 
  118.      *            源文件的编码 
  119.      * @return 
  120.      * @throws Exception 
  121.      */  
  122.     public static String getFileContentFromCharset(File file,  
  123.             String fromCharsetName) throws Exception {  
  124.         if (!Charset.isSupported(fromCharsetName)) {  
  125.             throw new UnsupportedCharsetException(fromCharsetName);  
  126.         }  
  127.         InputStream inputStream = new FileInputStream(file);  
  128.         InputStreamReader reader = new InputStreamReader(inputStream,  
  129.                 fromCharsetName);  
  130.         char[] chs = new char[(int) file.length()];  
  131.         reader.read(chs);  
  132.         String str = new String(chs).trim();  
  133.         reader.close();  
  134.         return str;  
  135.     }  
  136.   
  137.     /** 
  138.      * 以指定编码方式写文本文件,存在会覆盖 
  139.      *  
  140.      * @param file 
  141.      *            要写入的文件 
  142.      * @param toCharsetName 
  143.      *            要转换的编码 
  144.      * @param content 
  145.      *            文件内容 
  146.      * @throws Exception 
  147.      */  
  148.     public static void saveFile2Charset(File file, String toCharsetName,  
  149.             String content) throws Exception {  
  150.         if (!Charset.isSupported(toCharsetName)) {  
  151.             throw new UnsupportedCharsetException(toCharsetName);  
  152.         }  
  153.         OutputStream outputStream = new FileOutputStream(file);  
  154.         OutputStreamWriter outWrite = new OutputStreamWriter(outputStream,  
  155.                 toCharsetName);  
  156.         outWrite.write(content);  
  157.         outWrite.close();  
  158.     }  
posted @ 2015-04-15 13:00  小鲜肉成长记  阅读(326)  评论(0编辑  收藏  举报