Java - String getBytes() 方法



描述

Java String getBytes() 方法用於使用平臺的預設字元集將此字串編碼(轉換)為位元組序列,並將結果儲存到新的位元組陣列中。在 Java 中,陣列是一個包含相似資料型別元素的物件。位元組是記憶體中最小的可定址單元。

注意:int 資料型別的尺寸為 4 個位元組,char 資料型別的尺寸為 1 個位元組,依此類推。

getBytes() 方法接受字元集和字串作為引數,其中包含字元集和字元集名稱的值。如果字元集或字元集名稱無效,則會丟擲異常。

此方法具有三個具有不同引數的多型變體,如下面的語法所示。

語法

以下是Java String getBytes() 方法的語法:

public byte[] getBytes() // first syntax 
public byte[] getBytes(Charset charset) // second syntax
public byte[] getBytes(String charsetName) // third syntax

引數

  • charset − 這是十六位 Unicode 程式碼單元序列和位元組序列之間的命名對映。
  • charsetName − 這是字串。

返回值

此方法使用給定的值將此字串編碼為位元組序列,並以位元組陣列的形式返回結果。

獲取字串的位元組示例

如果我們不向方法傳遞預設字元集,它將透過獲取預設字元集本身來將此字串編碼為位元組序列。

在下面的程式中,我們使用值“Hello World”例項化字串類。然後,使用getBytes() 方法,我們嘗試將當前字串轉換為位元組序列並將它儲存在位元組陣列中。

package com.tutorialspoint;

import java.util.Arrays;

public class GetByte {
   public static void main(String[] args) {
      
      //instantiate the string class
      String str = new String("Hello World");
      System.out.println("The given string is: " + str);
      
      // create a byte array
      byte[] byte_arr;
      
      // using getBytes() method
      byte_arr = str.getBytes();
      
      // print the byte array value
      System.out.println("The byte array is: " + Arrays.toString(byte_arr));
   }
}

輸出

執行上述程式後,將產生以下結果:

The given string is: Hello World
The byte array is: [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]

使用 UTF-8 編碼獲取字串的位元組示例

如果我們將charset 傳遞給 getBytes() 方法,此方法將使用給定的字元集將此字串編碼為位元組序列。

在下面的示例中,我們使用值“Java”建立一個字串類的物件。使用getBytes() 方法,我們嘗試使用字元集“UTF-8”將當前字串編碼為位元組陣列。

package com.tutorialspoint;

import java.nio.charset.Charset;
import java.util.Arrays;

public class GetByte {
   public static void main(String[] args) {
      try {
         
         //create an object of the string class
         String str = new String("Java");
         System.out.println("The given string is: " + str);
         
         // copy the contents of the String to a byte array
         System.out.println("The charset is: " + Charset.forName("UTF-8"));
         byte[] arr = str.getBytes(Charset.forName("UTF-8"));
         System.out.println("The byte array is: ");
         for(int i = 0; i<arr.length; i++) {
            System.out.print(arr[i] + " ");
         }
      } catch (Exception e) {
         System.out.print(e.toString());
      }
   }
}

輸出

以下是上述程式的輸出:

The given string is: Java
The charset is: UTF-8
The byte array is: 
74 97 118 97

使用 UTF-16 編碼獲取字串的位元組示例

如果我們將字串charsetName 作為引數傳遞給該方法,它將使用給定的字元集名稱將此字串編碼為位元組序列。

在下面的程式中,我們使用值“TutorialsPoint”建立一個字串字面量。使用getBytes() 方法,我們嘗試使用字元集名稱“UTF-16”將當前字串編碼為位元組陣列。

package com.tutorialspoint;

import java.nio.charset.Charset;

public class GetByte {
   public static void main(String[] args) {
      try {
       
         // create a string literal
         String str= "TutoriaslPoint";
         System.out.println("The given string is: " + str);
         System.out.println("The charset name is: " + Charset.forName("UTF-16"));
         
         // byte array with charset
         byte bval[] = str.getBytes("UTF-8");
         // prints the byte array
         System.out.print("The byte array elements are: ");
         for (int i = 0; i < bval.length; i++) {
           System.out.print(bval[i] + " ");
         }
      } catch (Exception e) {
         e.printStackTrace();
         System.out.println("The exception is: " + e);
      }
   }
}

輸出

上述程式產生以下輸出:

The given string is: TutoriaslPoint
The charset name is: UTF-16
The byte array elements are: 84 117 116 111 114 105 97 115 108 80 111 105 110 116 

使用無效編碼獲取字串的位元組時出現異常的示例

如果給定的字元集或字元集名稱無效,getBytes() 方法將丟擲UnsupportedEncodingException

在下面的程式中,我們使用值“Tutorix”例項化一個字串類。然後,使用getBytes() 方法,我們嘗試使用字元集名稱“UTF-50”將當前字串轉換為位元組序列。由於字元集名稱無效,該方法將丟擲異常。

package com.tutorialspoint;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Arrays;

public class GetByte {
   public static void main(String[] args) {
      try {
         
         //instantiate the string class
         String str = new String("Tutorix");
         System.out.println("The given string is: " + str);
         System.out.println("The charset name is: " + Charset.forName("UTF-50"));
         
         // copy the contents of the String to a byte array
         byte[] arr = str.getBytes("UTF-50");
         System.out.println("The byte array is: " + Arrays.toString(arr));
      } catch (UnsupportedEncodingException e) {
         System.out.print(e.toString());
      }
   }
}

輸出

執行上述程式後,將產生以下輸出:

The given string is: Tutorix
Exception in thread "main" java.nio.charset.UnsupportedCharsetException: UTF-50
	at java.base/java.nio.charset.Charset.forName(Charset.java:528)
	at com.tutorialspoint.String.GetByte.main(GetByte.java:12)
java_lang_string.htm
廣告