Anyone with experience in Java knows that the way to print a string to a console is like this:
System.out.println("Hello, world.");
However, this method does not work well with non-ASCII characters. The following is some test code to demonstrate this:
import java.io.PrintStream;
public class Test {
public static void main (String[] argv) {
String unicodeMessage =
"\u7686\u3055\u3093\u3001\u3053\u3093\u306b\u3061\u306f";
PrintStream out = System.out;
out.println(unicodeMessage);
}
}
When compiled and executed, it looks like this:
iboook:~/Documents/codes tokek$ java Test
?????????
By looking at this, one might be misled to think that Unicode is not supported at the console for OS X. Well, it turns out that System.out prints Unicode strings using the MacRoman charset. One of the problems with this is that Japanese characters cannot be expressed with MacRoman, and the console uses UTF-8, not MacRoman. In order to surmount this problem, we will create a new PrintStream object that uses UTF-8.
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
public class Test {
public static void main (String[] argv) throws UnsupportedEncodingException {
String unicodeMessage =
"\u7686\u3055\u3093\u3001\u3053\u3093\u306b\u3061\u306f";
PrintStream out = new PrintStream(System.out, true, "UTF-8");
out.println(unicodeMessage);
}
}
(The PrintStream constructor is the reason why we need a throws UnsupportedEncodingException.)
皆さん、こんにちは
Here, the constructor options mean:
Mac OS X Hints
http://hints.macworld.com/article.php?story=20050208053951714