字符集的解码与编码API

48 阅读1分钟
public static void main(String[] args) throws Exception {
    String name = "abc我爱你中国";
    byte[] bytes = name.getBytes();//默认以UTF—8编码
    System.out.println(bytes.length);
    System.out.println(Arrays.toString(bytes));
    String s = new String(bytes);//默认以UTF-8进行解码
    System.out.println(s);

  byte[] bytes1 = name.getBytes("GBK");//指定以GBK进行编码
    System.out.println(bytes1.length);
    System.out.println(Arrays.toString(bytes1));
    String sr = new String(bytes1,"GBK");//指定以GBK进行解码
    System.out.println(sr);
}