unicode output java windows cmd

早过忘川 提交于 2021-02-05 07:18:06

问题


I'm new to java so excuse me if this is commong knowldge but I have searched hard and couldn't find anything helpful, relevant or understandable(Which is odd considering I'm a C developer!). My question is "How can I make java print a Unicode string in the windows shell ?". For simplicity say I have the hello world code in another language(ex: "سلام") and I want to display it in the shell (actually i want to get Unicode too, but first I have to figure this one out). This works perfectly in Intellij IDEA without any extra lines of code!

System.out.println("سلام");

but doesn't work in shell.

I'm seriously disappointed, I migrated from C just to get a better deal with Unicode!


回答1:


I used Intellij IDEA/Java 1.8 on Windows 10 and tried a bunch of things in a somewhat disorganized manner, but have it almost working. First, here's the code:

import java.io.PrintStream;
import java.io.UnsupportedEncodingException;

public class Java901App {
    public static void main(String[] args) {
        //System.out.println("Hello world!");
        //System.out.println("سلام");
        try{
            PrintStream outStream = new PrintStream(System.out, true, "UTF-8");
            outStream.println("Hello world!");
            outStream.println("سلام");
        } catch(UnsupportedEncodingException e){
            System.out.println("Caught exception: " + e.getMessage());
        }
    }
}
  • Note that the encoding for the PrintStream is set to UTF-8. See the selected answer for this post: Chinese Characters Displayed as Questions Marks in Mac Terminal

  • I added Arabic Script Supplemental Fonts to Windows based on this article from Microsoft: Why does some text display with square boxes in some apps on Windows 10? I'm not sure whether this was essential, but it definitely did no harm. I uninstalled Arabic Script Supplemental Fonts and nothing changed so this step was not necessary.

  • Before running the app from the console I called chcp 65001. That was definitely essential even though the PrintStream was defined to use UTF-8, as shown in the screen shot below.

  • I tried setting different fonts for the Command Prompt window by clicking the icon in the top left of the window, selecting Defaults form the dropdown menu and then clicking the Fonts tab. Some worked (e.g. Consolas) and some didn't (e.g. MS Gothic). Note this comment from a SuperUser post: In order for chcp 65001 to work, you must be using a TrueType font in the command prompt.

  • Here's some sample output:

  • So it is working except that the characters in the text you provided are being rendered in reverse order. Does anyone know how to fix that, presumably by somehow specifying in the Java source that the text is for a right-to-left language?


Update:

I amended the code so that the Persian text is rendered correctly in the Command Prompt window, though a side effect is that it no longer renders correctly when the code is run within the IDE. Here's the revised code:

public static void main(String[] args) {
    try{
        StringBuilder persianHello = new StringBuilder("سلام");
        PrintStream outStream = new PrintStream(System.out, true, "UTF-8");
        outStream.println("Hello world!");
        outStream.println(persianHello); // Renders backwards in console, but correctly in the IDE.
        byte directionality = Character.getDirectionality(persianHello.charAt(0));
        if (directionality == Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC) {
            outStream.println("Reversed string:" + persianHello.reverse()); // Renders correctly in console, but backwards in the IDE...
        }
    } catch(UnsupportedEncodingException e){
        System.out.println("Caught exception: " + e.getMessage());
    }
}

And here's the Command Prompt output using that code:

This fix is a hack; what's really needed is code that will behave correctly regardless of whether it is run from the IDE or the Command Prompt.



来源:https://stackoverflow.com/questions/48402025/unicode-output-java-windows-cmd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!