栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

在开发过程中,可能会遇到文件编码的转换,虽然说开发工具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"); 
   } 
 }); 
  } 
 
   
  public static void convert(String fileName, String fromCharsetName, 
      String toCharsetName) throws Exception { 
    convert(new File(fileName), fromCharsetName, toCharsetName, null); 
  } 
 
   
  public static void convert(File file, String fromCharsetName, 
      String toCharsetName) throws Exception { 
    convert(file, fromCharsetName, toCharsetName, null); 
  } 
 
   
  public static void convert(String fileName, String fromCharsetName, 
      String toCharsetName, FilenameFilter filter) throws Exception { 
    convert(new File(fileName), fromCharsetName, toCharsetName, filter); 
  } 
 
   
  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); 
      } 
    } 
  } 
 
   
  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; 
  } 
 
   
  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(); 
  } 
} 

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/140891.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号