String test = "中国";
int str0 = test.getBytes().length;
System.out.println("str1: length is " + str0 + " ," + new String(test.getBytes()));
int str1 = test.getBytes("ASCII").length;
System.out.println("str1: length is " + str1 + " ," + new String(test.getBytes("ASCII")));
int str2 = test.getBytes("GBK").length;
System.out.println("str2: length is " + str2 + " ," + new String(test.getBytes("GBK")));
int str3 = test.getBytes("UTF-8").length;
System.out.println("str3: length is " + str3 + " ," + new String(test.getBytes("UTF-8")));
这段代码打印出显示,
str1: length is 4 ,中国
str1: length is 2 ,??
str2: length is 4 ,中国
str3: length is 6 ,涓浗这里对字符串转成Byte流的规则有点不明白。用ASCII码每个字符只读一个字节出来,那么按什么规则读?用UTF-8每个字符读3个字节,按什么规则读?