Java String getBytes()方法具有什么功能呢?
Java String getBytes()方法的功能说明
Java String getBytes()方法的功能:
采用平台的默认字符集将此String编码转换为字节数组,并返回字节数组。
语法
public byte[] getBytes()
参数
此方法无参数返回值
- 返回转换后的字节数组
例
import java.io.*;
public class testClass {
public static void main(String args[]) {
String Str1 = new String("Welcome to java265.com");
try {
String Str2 = new String( Str1.getBytes( "UTF-8" ));
System.out.println("return: " + Str2 );
Str2 = new String (Str1.getBytes( "ISO-8859-1" ));
System.out.println("return " + Str2 );
} catch ( UnsupportedEncodingException e) {
System.out.println("Unsupported character set");
}
}
}
/*
以上代码运行后,将输出以下信息
return: Welcome to java265.com
return Welcome to java265.com
*/版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


