關於java中字元編碼的一點心得,可能對初學者有點協助

來源:互聯網
上載者:User
編碼|初學|心得 這是張孝祥老師的java就業培訓視頻教程裡面的一道題目(有所變動):
編寫下面的程式碼,分析和觀察程式的運行結果:

import java.io.*;
public class TestCodeIO {
      public static void main(String[] args) throws Exception{
            InputStreamReader isr = new InputStreamReader(System.in,"iso8859-1");
            BufferedReader br = new BufferedReader (isr);
            String strLine = br.readLine();
            br.close();
            isr.close();
            System.out.println(strLine);
      } 
}
運行程式後,輸入“中國”兩個字,輸出結果為 ???ú
請按照下面兩種方法修改上述程式,是輸入的中文能夠正常輸出
1。修改程式中的語句
              InputStreamReader isr = new InputStreamReader(System.in,"iso8859-1");
2。不修改上面的語句,修改下面的語句
              System.out.println(strLine);


第一種該法很簡單,只要改成下面這樣就可以了,這裡不詳細討論
            InputStreamReader isr = new InputStreamReader(System.in,"gb2312");


這裡我要詳細討論的是第二種該法怎麼改

起初我是這樣改的
          System.out.println(new String (strLine.getBytes(),"iso8859-1"));
輸入“中國”後輸出的結果雖然不是上面所述的亂碼,但是還是亂碼,顯然這種該法是不正確的!

這裡我要感謝 軟體民工  告訴我的正確改法,使我恍然大悟
           System.out.println(new String (strLine.getBytes("iso8859-1")));

這兩種改法究竟有什麼區別呢?為了方便大家閱讀,我先把正確和錯誤的改法帖出來:
import java.io.*;
     public class TestCodeIO {
           public static void main(String[] args) throws Exception{
                 InputStreamReader isr = new InputStreamReader(System.in,"iso8859-1");
                       //Create an InputStreamReader that uses the given charset decoder
                 BufferedReader br = new BufferedReader (isr);
                 String strLine = br.readLine();
                 br.close();
                 isr.close();
                 System.out.println(strLine);
                 System.out.println(new String (strLine.getBytes(),"iso8859-1"));//錯誤改法
                       //Encodes this String (strLine) into a sequence of bytes using the platform’s 
                       //default charset(gb2312) then constructs a new String by decoding the 
                      //specified array of bytes using the specified charset (iso8859-1)
                      //because this String (strLine) uses the charset decoder "iso8859-1",so it can
                      //only be encoded by "iso8859-1",cann’t be encoded by the platform’s default
                      //charset "gb2312",so this line is wrong.
                System.out.println(new String (strLine.getBytes("iso8859-1")));//正確改法
                     //Encodes this String (strLine) into a sequence of bytes using the named 
                     //charset (iso8859-1),then constructs a new String by decoding the 
                     //specified array of bytes using the platform’s default charset (gb2312).
                     //This line is right.
       } 
}

上面的英文注釋已經說得很清楚了,這裡我還是解釋一下吧:

首先是錯誤的改法  System.out.println(new String (strLine.getBytes(),"iso8859-1"));
這句代碼是將strLine中的字串用系統預設的編碼方式(這裡是gb2312)
轉換為位元組序列,然後用指定的編碼方式(這裡是iso8859-1)構造一個新的
String對象,並列印到螢幕上。
錯誤在哪裡呢? 
請注意這一段代碼  
InputStreamReader isr = new InputStreamReader(System.in,"iso8859-1");
BufferedReader br = new BufferedReader (isr);
String strLine = br.readLine();
這裡strLine儲存的內容是用指定的編碼方式(iso8859-1)儲存的,而轉換成位元組碼
的時候(這句代碼strLine.getBytes())卻使用了系統預設的gb2312編碼,所以當然就
輸出亂碼了!然後用gb2312編碼的位元組序列構建新的String對象的時候又使用了
iso8859-1編碼,所以輸出的亂碼和System.out.println(strLine)有所不同。


至於正確的改法就不用詳細說明了吧,首先將strLine用iso8859-1編碼方式轉換成位元組
序列,然後用系統預設的編碼方式(gb2312)構建新的String對象,然後列印輸出。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.