用java實現文字文件的ANSI編碼轉化為UTF-8
阿新 • • 發佈:2019-02-05
package cwj.bbb; import java.io.*; class StreamTest { public static void main(String[] args) throws IOException { /* * 檔案由ANSI轉化為UTF-8 * 需要用到流InputStreamReader和OutputStreamWriter * 這兩個流有charset功能 * */ File srcFile = new File("/home/cwjy1202/hadoop/javaTest/dali09_seg_pos.txt"); File destFile = new File("/home/cwjy1202/hadoop/javaTest/dali01_000_CWJ000.txt"); InputStreamReader isr = new InputStreamReader(new FileInputStream(srcFile), "GBK"); //ANSI編碼 OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(destFile), "UTF-8"); //存為UTF-8 int len = isr.read(); while(-1 != len) { osw.write(len); len = isr.read(); } //重新整理緩衝區的資料,強制寫入目標檔案 osw.flush(); osw.close(); isr.close(); } }
如有不對,請指正,自己剛剛學習java,搞了好久才搞定!