IO 流中编码和解码问题

编码表

  • ASCII : American Standard Code for Information Interchange
    • 使用一个字节的 7 位可以表示
  • ISO8859-1 : 拉丁码表. 欧洲码表
    • 使用一个字节的 8 位表示
  • GBK : 中文编码表
  • Unicode : 国际标准码, 融合了多种文字
    • 所有文字都用两个字节来表示
  • UTF-8 :最多用三个字节来表示一个字符
// 示例: 字符串的编解码

    // 字符串转换成字节数组, 编码
    String str = "你好";

    // 使用 GBK 编码, -60 -29 -70 -61
    byte[] buf = str.getBytes("GBK");

    // 使用 utf-8 编码, -28 -67 -96 -27 -91 -67
    byte[] buf1 = str.getBytes("utf-8");

    // 将字节数组转换成字符串
    String s1 = new String(buf,"GBK");

    String s2 = new String(buf1,"utf-8");


**参考资料** - [JavaSE 基础视频(毕向东)](https://www.bilibili.com/video/av3129288/#page=14) - [JDK 1.6 中文文档](http://tool.oschina.net/apidocs/apidoc?api=jdk-zh)
原文地址:https://www.cnblogs.com/linkworld/p/7519599.html