Java String 類 getBytes() 方法將字符串編碼為字節序列並將其保存在字節數組中。
簽名
getBytes() 方法有三種變體。字符串 getBytes() 方法的簽名或語法如下:
public byte[] getBytes()
public byte[] getBytes(Charset charset)
public byte[] getBytes(String charsetName)throws UnsupportedEncodingException
參數
charset /charsetName - 方法支持的字符集的名稱。
返回
字節序列。
異常拋出
UnsupportedEncodingException:當方法不支持提到的字符集時拋出。
內部實現
public byte[] getBytes() {
return StringCoding.encode(value, 0, value.length);
}
字符串類 getBytes() 方法示例
無參數 getBytes() 方法使用平台的默認字符集對字符串進行編碼,即 UTF-8。以下兩個示例顯示相同。
文件名:StringGetBytesExample.java
public class StringGetBytesExample{
public static void main(String args[]){
String s1="ABCDEFG";
byte[] barr=s1.getBytes();
for(int i=0;i<barr.length;i++){
System.out.println(barr[i]);
}
}}
輸出:
65 66 67 68 69 70 71
Java String 類 getBytes() 方法示例 2
文件名:StringGetBytesExample2.java
該方法返回一個字節數組,該數組可以再次傳遞給 String 構造函數以獲取 String。
public class StringGetBytesExample2 {
public static void main(String[] args) {
String s1 = "ABCDEFG";
byte[] barr = s1.getBytes();
for(int i=0;i<barr.length;i++){
System.out.println(barr[i]);
}
// Getting string back
String s2 = new String(barr);
System.out.println(s2);
}
}
輸出:
65 66 67 68 69 70 71 ABCDEFG
Java String 類 getBytes() 方法示例 3
以下示例顯示了對不同字符集的編碼。
文件名:StringGetBytesExample3.java
// Import statement
import java.io.*;
public class StringGetBytesExample3
{
// main method
public static void main(String argvs[])
{
// input string
String str = "Welcome to JavaTpoint.";
System.out.println("The input String is:");
System.out.println(str + "\n");
// inside try block encoding is
// being done using different charsets
try
{
16 - bit UCS Transformation format
byte[] byteArr = str.getBytes("UTF-16");
System.out.println("After converted into UTF-16 the String is:");
for (int j = 0; j < byteArr.length; j++)
{
System.out.print(byteArr[j]);
}
System.out.println("\n");
// Big Endian byte order, 16 - bit UCS Transformation format
byte[] byteArr1 = str.getBytes("UTF-16BE");
System.out.println("After converted into UTF-16BE the String is:");
for (int j = 0; j < byteArr1.length; j++)
{
System.out.print(byteArr1[j]);
}
System.out.println("\n");
// ISO Latin Alphabet
byte[] byteArr2 = str.getBytes("ISO-8859-1");
System.out.println("After converted into ISO-8859-1 the String is:");
for (int j = 0; j < byteArr2.length; j++)
{
System.out.print(byteArr2[j]);
}
System.out.println("\n");
// Little Endian byte order, 16 - bit UCS Transformation format
byte[] byteArr3 = str.getBytes("UTF-16LE");
System.out.println("After converted into UTF-16LE the String is:");
for (int j = 0; j < byteArr3.length; j++)
{
System.out.print(byteArr3[j]);
}
}
catch (UnsupportedEncodingException g)
{
System.out.println("Unsupported character set" + g);
}
}
}
輸出:
The input String is: Welcome to JavaTpoint. After converted into UTF-16 the String is: -2-10870101010809901110109010103201160111032074097011809708401120111010501100116046 After converted into UTF-16BE the String is: 0870101010809901110109010103201160111032074097011809708401120111010501100116046 After converted into ISO-8859-1 the String is: 871011089911110910132116111327497118978411211110511011646 After converted into UTF-16LE the String is: 8701010108099011101090101032011601110320740970118097084011201110105011001160460
Java String 類 getBytes() 方法示例 4
以下示例顯示當 getBytes() 方法不支持字符集時,將引發 UnsupportedEncodingException。
文件名:StringGetBytesExample4.java
public class StringGetBytesExample4
{
// main method
public static void main(String argvs[])
{
// input string
String str = "Welcome to JavaTpoint.";
System.out.println("The input String is:");
System.out.println(str + "\n");
// encoding into UTF - 17
byte[] byteArr = str.getBytes("UTF-17");
System.out.println("After converted into UTF-17 the String is:");
for (int j = 0; j < byteArr.length; j++)
{
System.out.print(byteArr[j]);
}
}
}
輸出:
/StringGetBytesExample4.java:11:error:unreported exception UnsupportedEncodingException; must be caught or declared to be thrown byte[] byteArr = str.getBytes("UTF-17"); ^ 1 error
相關用法
- Java String getBytes()用法及代碼示例
- Java String getChars()用法及代碼示例
- Java String valueOf()用法及代碼示例
- Java String substring()用法及代碼示例
- Java String replace()用法及代碼示例
- Java String contains()用法及代碼示例
- Java String copyValueOf()用法及代碼示例
- Java String isEmpty()用法及代碼示例
- Java String toString()用法及代碼示例
- Java String endsWith()用法及代碼示例
- Java String split()用法及代碼示例
- Java String lines()用法及代碼示例
- Java String repeat()用法及代碼示例
- Java String strip()用法及代碼示例
- Java String lastIndexOf()用法及代碼示例
- Java String equals()用法及代碼示例
- Java String replaceAll()用法及代碼示例
- Java String subSequence()用法及代碼示例
- Java String startsWith()用法及代碼示例
- Java String indexOf()用法及代碼示例
注:本文由純淨天空篩選整理自 Java String getBytes()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。