Java String getBytes()方法使用指定的字符集将此String编码为字节序列,并将结果存储到新的字节数组中。
语法
以下是此方法的语法 -
public byte[] getBytes(String charsetName) throws UnsupportedEncodingException
参数charsetName - 字符集的名称
返回值返回结果为字节数组。
示例import java.io.*;
public class Test {
public static void main(String args[]) {
String Str1 = new String("Welcome to Yiibai.com");
try {
String Str2 = new String( Str1.getBytes( "UTF-8" ));
System.out.println("Returned Value " + Str2 );
Str2 = new String (Str1.getBytes( "ISO-8859-1" ));
System.out.println("Returned Value " + Str2 );
} catch ( UnsupportedEncodingException e) {
System.out.println("Unsupported character set");
}
}
}
执行上面示例代码,得到以下结果:
Returned Value Welcome to Yiibai.com
Returned Value Welcome to Yiibai.com
¥ 我要打赏
纠错/补充
收藏
加QQ群啦,易百教程官方技术学习群
注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。