网上看到一篇写Properties的代码,但是不明白的是,无论是读还是写,它都将字符从先通过ISO-8859-1解码然后再转成GB2312这是什么呢?private static String ISO2GB(String source) {
if (source == null || source.length() == 0) {
return "";
} String target = source;
try {
target = new String(source.getBytes("8859_1"), "GB2312");
} catch (Exception e) {
e.printStackTrace();
System.out.println("由ISO-8859-1到GB2312转码失败!");
}
return target;
}

解决方案 »

  1.   

    网络传输的默认字符集都是iso,所以先用iso获取然后转码程gbk
      

  2.   

    人家问的是Properties类的用法,和网络有啥关系?再说了网络里传输的东西都是二进制和所谓的默认字符集也没任何关系。最正确的解释在java的api文档里The load(Reader) / store(Writer, String) methods load and store properties from and to a character based stream in a simple line-oriented format specified below. The load(InputStream) / store(OutputStream, String) methods work the same way as the load(Reader)/store(Writer, String) pair, except the input/output stream is encoded in ISO 8859-1 character encoding. Characters that cannot be directly represented in this encoding can be written using Unicode escapes ; only a single 'u' character is allowed in an escape sequence. The native2ascii tool can be used to convert property files to and from other character encodings.这是英文原文,看不懂的话找中文的api文档对照一下吧。
      

  3.   

    Properties文件读取用的字符集都是ISO-8859-1的,必须做字符集之间的转换,否则会乱码
      

  4.   

    那UTF8呢,我们项目里都用它呢
    其它字符编码不用,无乱码现象
      

  5.   

    load 和 store 方法按下面所指定的、简单的面向行的格式加载和存储属性。此格式使用 ISO 8859-1 字符编码。可以使用 Unicode 转义符来编写此编码中无法直接表示的字符;转义序列中只允许单个 'u' 字符。可使用 native2ascii 工具对属性文件和其他字符编码进行相互转换。