使用平台默认字符集进行编码
//定义一个字符串
String s = "中国";
//编码
byte[] bys = s.getBytes();
System.out.println(Arrays.toString(bys));
//解码
String ss = new String(bys);
System.out.println(ss);
指定字符集进行编码
// 使用指定字符集解码
String ss= new String(bys, "UTF-8");
System.out.println(ss);
String sss= new String(bys, "GBK");
System.out.println(sss);
使用不同的字符集解码所呈现的结果不一样, 一般情况下, 用什么方式编码就用什么方式解码,否则就会出现乱码.