Java NIO之Charset類字元編碼對象

來源:互聯網
上載者:User

標籤:define   表之間   sort   struct   rap   表示   cto   byte   encode   

介紹
java中使用Charset來表示編碼對象
This class defines methods for creating decoders and encoders and for retrieving the various names associated with a charset.   Instances of this class are immutable.This class also defines static methods for testing whether a particular charset is supported, for locating charset instances by name, and for constructing a map that contains every charset  for which support is available in the current Java virtual machine.
Charset常用靜態方法
public static Charset forName(String charsetName)//通過編碼類別型獲得Charset對象public static SortedMap<String,Charset> availableCharsets()//獲得系統支援的所有編碼方式public static Charset defaultCharset()//獲得虛擬機器預設的編碼方式public static boolean isSupported(String charsetName)//判斷是否支援該編碼類別型

 

Charset常用普通方法
public final String name()//獲得Charset對象的編碼類別型(String)public abstract CharsetEncoder newEncoder()//獲得編碼器對象public abstract CharsetDecoder newDecoder()//獲得解碼器對象. . . 還有很多方法
Charset應用案列

獲得本機支援的所有編碼方式

public void testGetAvailableCharsets() {    // 獲得本機所有編碼格式    Map<String, Charset> charsets = Charset.availableCharsets();    // 迭代遍曆出編碼方式    for (Entry<String, Charset> entry : charsets.entrySet()) {        System.out.println(entry.getKey() + " : " + entry.getValue().name());    }}

 

獲得JVM虛擬機器預設編碼方式

// 獲得JVM預設編碼方式Charset charset=Charset.defaultCharset();

 

使用編碼器和解碼器進行字元編碼和解碼

public void testEncoderAndDecoder() throws Exception{    //使用Charset進行編碼和解碼    CharsetEncoder encoder=Charset.forName("GBK").newEncoder();    CharsetDecoder decoder=Charset.forName("GBK").newDecoder();    ByteBuffer byteBuffer=encoder.encode(CharBuffer.wrap("中國編碼".toCharArray()));    CharBuffer charBuffer=decoder.decode(byteBuffer);    String string=charBuffer.toString();    System.out.println(string);}
備忘:寫編碼方式時候最好使用全大寫字元比如:UTF-8、GBK。通常情況下大小寫都能識別備忘:java中關於字元編碼問題,通常藉助String構造方法或URLEncoder/URLDecoder,或則使用Charset的編碼器和解碼器。
總結
編碼和解碼問題是所有程式員都會面臨的問題,尤其是對於非英語系國家的程式員更是如此。只有理解清楚字元編碼原理才能在今後程式開發過程即使遇到編碼問題也能夠處之泰然。備忘:備忘電腦只能識別位元字,因此如果想要讓電腦識別出自然語言文字自然就需要存在一張位元字和自然語言的映射表。每次編碼和解碼時都要查詢這張映射表。

舉例:

假如使用GBK碼錶對"中"字編碼結果是183(十進位表示)如果你使用UTF-8碼錶來解碼的話,查出來的183對應的是"國"字。結果就是錯的了,所以編碼和解碼對應的碼錶一定要相同才能夠解碼正確(除非碼錶之間有內含項目關聯性,比如UTF-8已經包含了ASCII碼錶,那麼解碼就沒問題)
參考

1、https://en.wikipedia.org/wiki/Character_encoding
2、http://www.ruanyifeng.com/blog/2010/02/url_encoding.html
3、http://www.ruanyifeng.com/blog/2007/10/ascii_unicode_and_utf-8.html
4、http://polaris.blog.51cto.com/1146394/377468/

Java NIO之Charset類字元編碼對象

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.