MacOSX下Terminal默认编码为MacRoman,会产生乱码

测试:System.out.println(“这是一个中文字串”);

解决方法

1.运行时加入参数Dfile.encoding:

java -Dfile.encoding=UTF8 ClassName

2.改变system.out编码:

PrintStream out = null;
try {
    out = new PrintStream(System.out, true, "UTF-8");
} catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
out.println("这是一个中文字串");

ps.不解的时候可以查看下当前编码模式

Properties properties = System.getProperties();
System.out.println(properties.getProperty("file.encoding"));