JAVA 的字符编码之间相互转化

6 min read
public class Main {

  public static void main(String[] args) throws UnsupportedEncodingException {
    String s = new String("A中文");

    //编码
    byte[] bytes = s.getBytes("GBK");

    // 解码,指定编码格式
    String gbk = new String(bytes, "GBK");
    System.out.println(gbk);

    // 查看默认的编码
    String s1 = Charset.defaultCharset().toString();
    System.out.println(s1);  // utf-8

  }

}