当前位置: 移动技术网 > IT编程>开发语言>Java > java实现文件编码转换的方法

java实现文件编码转换的方法

2019年07月19日  | 移动技术网IT编程  | 我要评论

2013年5月9日,虚拟内存的设置,浪漫婚礼策划

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

package com.mikan.stuff; 
 
import java.io.file; 
import java.io.fileinputstream; 
import java.io.fileoutputstream; 
import java.io.filenamefilter; 
import java.io.inputstream; 
import java.io.inputstreamreader; 
import java.io.outputstream; 
import java.io.outputstreamwriter; 
import java.nio.charset.charset; 
import java.nio.charset.unsupportedcharsetexception; 
 
public class filecharsetconverter { 
 
  public static void main(string[] args) throws exception { 
    convert("d:\\stuff\\src\\main\\java\\com\\mikan\\stuff\\test.txt", 
        "gbk", "utf-8", new filenamefilter() { 
          @override 
          public boolean accept(file dir, string name) { 
            return name.endswith("txt"); 
          } 
        }); 
  } 
 
  /** 
   * 把指定文件或目录转换成指定的编码 
   * 
   * @param filename 
   *      要转换的文件 
   * @param fromcharsetname 
   *      源文件的编码 
   * @param tocharsetname 
   *      要转换的编码 
   * @throws exception 
   */ 
  public static void convert(string filename, string fromcharsetname, 
      string tocharsetname) throws exception { 
    convert(new file(filename), fromcharsetname, tocharsetname, null); 
  } 
 
  /** 
   * 把指定文件或目录转换成指定的编码 
   * 
   * @param file 
   *      要转换的文件或目录 
   * @param fromcharsetname 
   *      源文件的编码 
   * @param tocharsetname 
   *      要转换的编码 
   * @throws exception 
   */ 
  public static void convert(file file, string fromcharsetname, 
      string tocharsetname) throws exception { 
    convert(file, fromcharsetname, tocharsetname, null); 
  } 
 
  /** 
   * 把指定文件或目录转换成指定的编码 
   * 
   * @param file 
   *      要转换的文件或目录 
   * @param fromcharsetname 
   *      源文件的编码 
   * @param tocharsetname 
   *      要转换的编码 
   * @param filter 
   *      文件名过滤器 
   * @throws exception 
   */ 
  public static void convert(string filename, string fromcharsetname, 
      string tocharsetname, filenamefilter filter) throws exception { 
    convert(new file(filename), fromcharsetname, tocharsetname, filter); 
  } 
 
  /** 
   * 把指定文件或目录转换成指定的编码 
   * 
   * @param file 
   *      要转换的文件或目录 
   * @param fromcharsetname 
   *      源文件的编码 
   * @param tocharsetname 
   *      要转换的编码 
   * @param filter 
   *      文件名过滤器 
   * @throws exception 
   */ 
  public static void convert(file file, string fromcharsetname, 
      string tocharsetname, filenamefilter filter) throws exception { 
    if (file.isdirectory()) { 
      file[] filelist = null; 
      if (filter == null) { 
        filelist = file.listfiles(); 
      } else { 
        filelist = file.listfiles(filter); 
      } 
      for (file f : filelist) { 
        convert(f, fromcharsetname, tocharsetname, filter); 
      } 
    } else { 
      if (filter == null 
          || filter.accept(file.getparentfile(), file.getname())) { 
        string filecontent = getfilecontentfromcharset(file, 
            fromcharsetname); 
        savefile2charset(file, tocharsetname, filecontent); 
      } 
    } 
  } 
 
  /** 
   * 以指定编码方式读取文件,返回文件内容 
   * 
   * @param file 
   *      要转换的文件 
   * @param fromcharsetname 
   *      源文件的编码 
   * @return 
   * @throws exception 
   */ 
  public static string getfilecontentfromcharset(file file, 
      string fromcharsetname) throws exception { 
    if (!charset.issupported(fromcharsetname)) { 
      throw new unsupportedcharsetexception(fromcharsetname); 
    } 
    inputstream inputstream = new fileinputstream(file); 
    inputstreamreader reader = new inputstreamreader(inputstream, 
        fromcharsetname); 
    char[] chs = new char[(int) file.length()]; 
    reader.read(chs); 
    string str = new string(chs).trim(); 
    reader.close(); 
    return str; 
  } 
 
  /** 
   * 以指定编码方式写文本文件,存在会覆盖 
   * 
   * @param file 
   *      要写入的文件 
   * @param tocharsetname 
   *      要转换的编码 
   * @param content 
   *      文件内容 
   * @throws exception 
   */ 
  public static void savefile2charset(file file, string tocharsetname, 
      string content) throws exception { 
    if (!charset.issupported(tocharsetname)) { 
      throw new unsupportedcharsetexception(tocharsetname); 
    } 
    outputstream outputstream = new fileoutputstream(file); 
    outputstreamwriter outwrite = new outputstreamwriter(outputstream, 
        tocharsetname); 
    outwrite.write(content); 
    outwrite.close(); 
  } 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网