The NoteBook of EricKong

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            611 Posts :: 1 Stories :: 190 Comments :: 0 Trackbacks

           

          有一個UTF-8編碼的文本文件,用FileReader讀取到一個字符串,然后轉換字符集:str=new String(str.getBytes(),"UTF-8");結果大部分中文顯示正常,但最后仍有部分漢字顯示為問號!
          Java代碼 
          public static List<String> getLines(String fileName){  
                  List<String> lines=new ArrayList<String>();  
                  try {  
                      BufferedReader br = new BufferedReader(new FileReader(fileName));  
                      String line = null;  
                      while ((line = br.readLine()) != null) {  
                          lines.add(new String(line.getBytes("GBK"),"UTF-8"));  
                      }  
                      br.close();  
                  } catch (FileNotFoundException e) {  
                  }catch (IOException e) {}  
                  return lines;  
              } 

          public static List<String> getLines(String fileName){
            List<String> lines=new ArrayList<String>();
            try {
             BufferedReader br = new BufferedReader(new FileReader(fileName));
             String line = null;
             while ((line = br.readLine()) != null) {
              lines.add(new String(line.getBytes("GBK"),"UTF-8"));
             }
             br.close();
            } catch (FileNotFoundException e) {
            }catch (IOException e) {}
            return lines;
           }

           

          文件讀入時是按OS的默認字符集即GBK解碼的,我先用默認字符集GBK編碼str.getBytes(“GBK”),此時應該還原為文件中的字節序列了,然后再按UTF-8解碼,生成的字符串按理說應該就應該是正確的。

          為什么結果中還是有部分亂碼呢?
          問題出在FileReader讀取文件的過程中,FileReader繼承了InputStreamReader,但并沒有實現父類中帶字符集參數的構造函數,所以FileReader只能按系統默認的字符集來解碼,然后在UTF-8 -> GBK -> UTF-8的過程中編碼出現損失,造成結果不能還原最初的字符。

          原因明確了,這個問題解決起來并不困難,用InputStreamReader代替FileReader,InputStreamReader isr=new InputStreamReader(new FileInputStream(fileName),"UTF-8");這樣讀取文件就會直接用UTF-8解碼,不用再做編碼轉換。
          Java代碼 
          public static List<String> getLines(String fileName){  
                  List<String> lines=new ArrayList<String>();  
                  try {  
                      BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(fileName),"UTF-8"));  
                      String line = null;  
                      while ((line = br.readLine()) != null) {  
                          lines.add(line);  
                      }  
                      br.close();  
                  } catch (FileNotFoundException e) {  
                  }catch (IOException e) {}  
                  return lines;  
              } 

           

          posted on 2012-04-28 21:00 Eric_jiang 閱讀(5531) 評論(2)  編輯  收藏 所屬分類: Java

          Feedback

          # re: FileReader讀取中文txt文件編碼丟失問題(亂碼) 2012-04-29 20:12 Eric_jiang
          http://code.google.com/p/sqlite-manager/  回復  更多評論
            

          # re: FileReader讀取中文txt文件編碼丟失問題(亂碼) 2013-02-20 15:12 網絡記事本
          FileReader 功能還是不行。
          謝謝指點,我已經更換為 FileInputStream 了,這個支持設置編碼。  回復  更多評論
            

          主站蜘蛛池模板: 黔南| 竹山县| 晴隆县| 万源市| 常宁市| 通州区| 郓城县| 孝昌县| 罗甸县| 同德县| 塔河县| 安多县| 德庆县| 外汇| 时尚| 武山县| 青海省| 德令哈市| 丽江市| 清原| 樟树市| 彭阳县| 湖口县| 靖远县| 克东县| 桂平市| 龙胜| 抚松县| 安西县| 盐山县| 安义县| 阳朔县| 扎囊县| 延川县| 新闻| 黄大仙区| 鹤岗市| 宁夏| 庐江县| 衡南县| 都昌县|