国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

java實現文件編碼轉換的方法

2024-07-14 08:40:50
字體:
來源:轉載
供稿:網友

在開發過程中,可能會遇到文件編碼的轉換,雖然說開發工具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();   } } 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阜康市| 万全县| 基隆市| 额尔古纳市| 专栏| 正定县| 东安县| 深州市| 芒康县| 临沂市| 安塞县| 抚松县| 台中市| 屯留县| 如皋市| 肥城市| 商洛市| 通城县| 新龙县| 砚山县| 林周县| 贵南县| 都兰县| 湖南省| 廊坊市| 黔江区| 寻乌县| 深泽县| 嵊州市| 无极县| 新蔡县| 高平市| 修水县| 玉屏| 梁山县| 华容县| 石台县| 江山市| 浪卡子县| 泸西县| 磐安县|