环境:英文XP SP2,Eclipse 3.2,JDK 1.5使用测试代码:
String encoding = System.getProperty("file.encoding");
System.out.println("file.encoding:" + encoding);
System.out.println("sun.jnu.encoding:" + System.getProperty("sun.jnu.encoding"));

String testString = "现在开始测试";

System.out.println(testString);

System.out.println(new String(testString.getBytes("ISO-8859-1"),"gb2312"));
System.out.println(new String(testString.getBytes("UTF8"), "gb2312"));
System.out.println(new String(testString.getBytes("GB2312"),"gb2312"));
System.out.println(new String(testString.getBytes("GBK"), "gb2312"));
System.out.println(new String(testString.getBytes("BIG5"), "gb2312"));
System.out.println(new String(testString.getBytes("Cp1252"),"gb2312"));

System.clearProperty("file.encoding");
System.setProperty("file.encoding", "UTF-8");
System.setProperty("sun.jnu.encoding", "UTF-8");
System.out.println("file.encoding:" + System.getProperty("file.encoding"));
System.out.println("sun.jnu.encoding:" + System.getProperty("sun.jnu.encoding"));

System.out.println(testString);
// 这一段出现了乱码,那么不妨用穷举法猜测一下它的实际编码格式。
System.out.println(new String(testString.getBytes("ISO-8859-1"),"gb2312"));
System.out.println(new String(testString.getBytes("UTF8"), "gb2312"));
System.out.println(new String(testString.getBytes("GB2312"),"gb2312"));
System.out.println(new String(testString.getBytes("GBK"), "gb2312"));
System.out.println(new String(testString.getBytes("BIG5"), "gb2312"));
System.out.println(new String(testString.getBytes("Cp1252"),"gb2312"));
Console log为:
file.encoding:Cp1252
sun.jnu.encoding:GBK
??????
??????
?????????????
??????
??????
??b??l??
??????
file.encoding:UTF-8
sun.jnu.encoding:UTF-8
??????
??????
?????????????
??????
??????
??b??l??
??????请大家来支支招,这类的Console文中的怎么处理。

解决方案 »

  1.   

    String testString = "现在开始测试";  你这段出的问题。
      

  2.   

    在英文控制台下,总是显示乱码。但是,
    这些乱码分为2类,楼主心中一定要分清楚:1. 一种情况是:程序的处理过程本身就是错的,因此显示出来乱码。
    2. 另一种情况是:程序是对的,由于控制台是英文的无法显示汉字而显示乱码。第二种情况中,程序是对的,也就是说,虽然在控制台显示是乱码,但如果作为服务程序,当客户端是中文环境是,能够正常显示中文。
    而第一种情况中,就算是作为服务程序,客户端是中文时仍然会是乱码。因此,光凭着这样试是无法拿出最好的方案来的。推荐楼主从概念入手,掌握其中的原理:
    http://www.regexlab.com/zh/encoding.htm
      

  3.   

    代码应该没有问题,测试在中文XP SP2中是没有问题的,Console log:
    file.encoding:GBK
    sun.jnu.encoding:GBK
    现在开始测试
    ??????
    ??板?ㄥ??濮?娴?璇?
    现在开始测试
    现在开始测试
    ??b??l??
    ??????
    file.encoding:UTF-8
    sun.jnu.encoding:UTF-8
    现在开始测试
    ??????
    ??板?ㄥ??濮?娴?璇?
    现在开始测试
    现在开始测试
    ??b??l??
    ??????目前还是在英文环境怎么解决
      

  4.   

    此外PrintWriter out在英文XP中println的中文也是乱码,而在中文XP却没有问题,此类环境的编码怎么设置