真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

怎么在java中對(duì)文件編碼進(jìn)行轉(zhuǎn)換

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)怎么在java中對(duì)文件編碼進(jìn)行轉(zhuǎn)換,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

創(chuàng)新互聯(lián)公司是一家專業(yè)提供哈密企業(yè)網(wǎng)站建設(shè),專注與做網(wǎng)站、網(wǎng)站建設(shè)、H5響應(yīng)式網(wǎng)站、小程序制作等業(yè)務(wù)。10年已為哈密眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進(jìn)行中。

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"); 
          } 
        }); 
  } 
 
  /** 
   * 把指定文件或目錄轉(zhuǎn)換成指定的編碼 
   * 
   * @param fileName 
   *      要轉(zhuǎn)換的文件 
   * @param fromCharsetName 
   *      源文件的編碼 
   * @param toCharsetName 
   *      要轉(zhuǎn)換的編碼 
   * @throws Exception 
   */ 
  public static void convert(String fileName, String fromCharsetName, 
      String toCharsetName) throws Exception { 
    convert(new File(fileName), fromCharsetName, toCharsetName, null); 
  } 
 
  /** 
   * 把指定文件或目錄轉(zhuǎn)換成指定的編碼 
   * 
   * @param file 
   *      要轉(zhuǎn)換的文件或目錄 
   * @param fromCharsetName 
   *      源文件的編碼 
   * @param toCharsetName 
   *      要轉(zhuǎn)換的編碼 
   * @throws Exception 
   */ 
  public static void convert(File file, String fromCharsetName, 
      String toCharsetName) throws Exception { 
    convert(file, fromCharsetName, toCharsetName, null); 
  } 
 
  /** 
   * 把指定文件或目錄轉(zhuǎn)換成指定的編碼 
   * 
   * @param file 
   *      要轉(zhuǎn)換的文件或目錄 
   * @param fromCharsetName 
   *      源文件的編碼 
   * @param toCharsetName 
   *      要轉(zhuǎn)換的編碼 
   * @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); 
  } 
 
  /** 
   * 把指定文件或目錄轉(zhuǎn)換成指定的編碼 
   * 
   * @param file 
   *      要轉(zhuǎn)換的文件或目錄 
   * @param fromCharsetName 
   *      源文件的編碼 
   * @param toCharsetName 
   *      要轉(zhuǎn)換的編碼 
   * @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); 
      } 
    } 
  } 
 
  /** 
   * 以指定編碼方式讀取文件,返回文件內(nèi)容 
   * 
   * @param file 
   *      要轉(zhuǎn)換的文件 
   * @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; 
  } 
 
  /** 
   * 以指定編碼方式寫文本文件,存在會(huì)覆蓋 
   * 
   * @param file 
   *      要寫入的文件 
   * @param toCharsetName 
   *      要轉(zhuǎn)換的編碼 
   * @param content 
   *      文件內(nèi)容 
   * @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中對(duì)文件編碼進(jìn)行轉(zhuǎn)換了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。


標(biāo)題名稱:怎么在java中對(duì)文件編碼進(jìn)行轉(zhuǎn)換
網(wǎng)頁(yè)網(wǎng)址:http://www.weahome.cn/article/gcpggg.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部