1、介紹
Java的getBytes()方法,是將String類型轉換成byte數組的方法之一。在Java中,字符串是使用Unicode編碼的,而位元組數組是使用一些標準字符集編碼的。因此,在Java中,字符串到位元組數組需要進行編碼轉換。本文將深入分析Java的getBytes()方法,幫助讀者更好地理解該方法的使用。
2、正文
2.1、getBytes(String charsetName)方法
Java的getBytes(String charsetName)方法,將String對象轉換成byte數組。該方法可以指定編碼方式,Java支持的編碼方式包括UTF-8、GBK等。
String str = "深入分析Java getBytes方法"; byte[] bytes = null; try { bytes = str.getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } System.out.println(Arrays.toString(bytes));
對於上述代碼,str是要轉換成byte數組的字符串,使用try-catch包含指定編碼方式方法getBytes(),該方法返回一個表示給定字符集的位元組數組,通過打印可以看到以下輸出:
[-27, -104, -95, -28, -67, -96, -24, -87, -110, -27, -84, -123, 74, 97, 118, 97, 32, 103, 101, 116, 66, 121, 116, 101, 115, -27, -77, -127, -25, -80, -127, -26, -120, -111, -27, -101, -67]
該輸出為UTF-8編碼下的位元組數組進行每個位元組的輸出,每個中文字符佔用三個位元組,數字和字母均佔用一個位元組。
2.2、getBytes()方法
Java的getBytes()方法可以無參使用,使用默認的編碼方式將String對象轉換成byte數組。
String str = "深入分析Java getBytes方法"; byte[] bytes = str.getBytes(); System.out.println(Arrays.toString(bytes));
對於上述代碼,使用無參方式調用getBytes()方法,該方法返回採用默認編碼方式(UTF-8)字符串str的位元組數組。通過打印可以看到以下輸出:
[-27, -104, -95, -28, -67, -96, -24, -87, -110, -27, -84, -123, 74, 97, 118, 97, 32, 103, 101, 116, 66, 121, 116, 101, 115, -27, -77, -127, -25, -80, -127, -26, -120, -111, -27, -101, -67]
與上述getBytes(String charsetName)方法實現相同。
2.3、String(byte[] bytes)和String(byte[] bytes, String charsetName)方法
String(byte[] bytes)和String(byte[] bytes, String charsetName)方法,都是將byte數組轉換成String類型的方法。其中,String(byte[] bytes)方法默認使用UTF-8編碼方式。
byte[] bytes = {-27, -104, -95, -28, -67, -96, -24, -87, -110, -27, -84, -123, 74, 97, 118, 97, 32, 103, 101, 116, 66, 121, 116, 101, 115, -27, -77, -127, -25, -80, -127, -26, -120, -111, -27, -101, -67}; String str1 = new String(bytes); String str2 = null; try { str2 = new String(bytes, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } System.out.println(str1); System.out.println(str2);
對於上述代碼,使用byte數組初始化String類型str1對象,則str1默認使用UTF-8方式來進行編碼,輸出為:
深入分析Java getBytes方法Jav
使用try-catch包含指定編碼方式方法,獲取該byte數組的字符串,則str2與上述對應輸出一致。
2.4、字符集轉換
在Java編程中,偶爾會遇到不同編碼方式間的轉換。使用字符串的getBytes方法進行編碼轉換、使用String的構造器完成解碼轉換字符串,該方式適合自定義其他非Java支持標準字符集的編碼方式。
String str = "深入分析Java getBytes方法"; Charset charset = Charset.forName("GBK"); ByteBuffer byteBuffer = charset.encode(str); System.out.println(Arrays.toString(byteBuffer.array())); CharBuffer charBuffer = charset.decode(byteBuffer); System.out.println(charBuffer.toString());
對於上述代碼,通過使用Charset類定義字符集為GBK方式。將字符串str進行GBK編碼,再通過ByteBuffer轉換成CharBuffer進行解碼,輸出與原字符串str一致。
3、小結
Java的getBytes方法是將String類型轉換成byte數組的方法之一,在Java中,字符串是Unicode編碼的,而位元組數組是使用一些標準字符集編碼的。在Java中,字符串到位元組數組需要進行編碼轉換。本文對Java的getBytes()方法進行了詳細解析,同時給出了相關代碼,希望能幫助讀者更好地理解該方法在位元組數組轉換方面的應用。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/309753.html