温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在java中对文件编码进行转换

发布时间:2021-02-20 17:10:22 来源:亿速云 阅读:257 作者:Leah 栏目:编程语言

这期内容当中小编将会给大家带来有关怎么在java中对文件编码进行转换,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

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(); 
  } 
}

上述就是小编为大家分享的怎么在java中对文件编码进行转换了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI