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 了,這個支持設置編碼。  回復  更多評論
            

          主站蜘蛛池模板: 江永县| 枞阳县| 惠州市| 穆棱市| 于田县| 彝良县| 龙门县| 乌拉特中旗| 江油市| 涿鹿县| 光泽县| 高雄市| 思南县| 明星| 淳安县| 枝江市| 铜川市| 永宁县| 枣阳市| 宁乡县| 岚皋县| 扎鲁特旗| 福安市| 佳木斯市| 巴中市| 襄城县| 吴旗县| 从化市| 马尔康县| 长宁县| 莱西市| 阳原县| 江孜县| 苍梧县| 潜山县| 张家界市| 宜春市| 贡觉县| 阿尔山市| 东山县| 墨脱县|