Print Arabic or other charset in System.out

2019-02-26 02:47发布

I'm tyring to print a String with Arabic characters:

private static void print(String msg, Object... args) {
    try {
        PrintStream ps = new PrintStream(System.out, true, "ISO-8859-6");
        ps.println(String.format(msg, args));
    } catch (UnsupportedEncodingException error) {
        System.err.println(error);
        System.exit(0);
    }
}

However, I see from the Eclipse log console that the Arabic characters as displayed as series of these kind of characters èååêÒÉ

What could be missing in my code?

2条回答
疯言疯语
2楼-- · 2019-02-26 03:01

Make sure the console you use to display the output is also encoded in ISO-8859-6. In Eclipse , you need to go to Run Configuration > Common to do this.

查看更多
小情绪 Triste *
3楼-- · 2019-02-26 03:21

try this:

private static void print(String msg, Object... args) {
    try {
        PrintStream ps = new PrintStream(System.out, true, "UTF-8");
        ps.println(String.format(msg, args));
    } catch (UnsupportedEncodingException error) {
        System.err.println(error);
        System.exit(0);
    }
}

public static void main (String[] args) throws UnsupportedEncodingException {
    String arabicString = "كيف حالك";
    print(arabicString);
}
查看更多
登录 后发表回答