扫码一下
查看教程更方便
getBytes() 方法使用平台的默认字符集将此字符串编码为字节序列,并将结果存储到新的字节数组中。
public byte[] getBytes()
无
此方法返回结果字节数组。
public class Main { public static void main(String args[]) { String Str1 = new String("Welcome to jiyik.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 jiyik.com
Returned Value Welcome to jiyik.com