方法 | 说明 |
---|
byte[] getBytes() | 使用平台的默认字符集将String编码为一系列字节,将结果存储到新的字节数组中 UTF-8 |
byte[] getBytes(String charsetName) | 使用指定的字符集将String编码为一系列字节,将结果存储到新的字节数组中 |
String(byte[] bytes) | 通过使用平台的默认字符集解码为指定的字节数组来构建新的String |
String(byte[] bytes, String charsetName) | 通过使用指定的字符集解码为指定的字节数组来构建新的String |
private static void Test1() throws UnsupportedEncodingException {
String data = "a我b";
byte[] bytes = data.getBytes();
System.out.println(Arrays.toString(bytes));
byte[] bytes1 = data.getBytes("GBK");
System.out.println(Arrays.toString(bytes1));
String s = new String(bytes);
System.out.println(s);
String s1 = new String(bytes1, "GBK");
System.out.println(s1);
}