import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Arrays;
public class Test {
public static void main(String[] args) throws UnsupportedEncodingException {
char x = '编'; // char 占用两个字节 也就是16bit.
// 获取JVM 默认字符集
System.out.println(Charset.defaultCharset()); //UTF-8
System.out.println(System.getProperty("file.encoding"));//UTF-8
String str = "编";
System.out.println("##字符串转换成byte数组");
byte[] defaultByteArray = str.getBytes();
byte[] gbkByteArray = str.getBytes("GBK");
byte[] utfByteArray = str.getBytes("UTF-8");
System.out.println("defaultByteArray:" + Arrays.toString(defaultByteArray)); //defaultByteArray:[-25, -68, -106]
System.out.println("gbkByteArray:" + Arrays.toString(gbkByteArray)); //gbkByteArray:[-79, -32]
System.out.println("utfByteArray:" + Arrays.toString(utfByteArray)); //utfByteArray:[-25, -68, -106]
System.out.println("##byte数组转换成字符串");
String defaultStr = new String(defaultByteArray); // utf-8 到 utf-8 没有问题
String gbkStr = new String(defaultByteArray, "GBK"); // utf-8 到 gbk 即: [-25, -68, -106] 到 [-79, -32]
String utfStr = new String(defaultByteArray, "UTF-8"); // utf-8 到 utf-8 没有问题
System.out.println("defaultStr:" + defaultStr); // defaultStr:编
System.out.println("gbkStr:" + gbkStr); // gbkStr:缂� 这里乱码的原因是gbk是两个字节表示
System.out.println("utfStr:" + utfStr); // utfStr:编
byte[] bytes = {-25, -68};
System.out.println(new String(bytes, "GBK") ); // 缂
}
}