Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Console input (win), how is the charset convertion working?

Code below, non-ascii chars output garbage - InputStreamReader in the below example doesn't take charset as an argument.

BufferedReader console = new BufferedReader( new InputStreamReader(System.in));
String inp = console.readLine();
System.out.println(inp.toUpperCase());

Being os-independent, how does Java solve all different possible charset configurations regarding console prompt input?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
126 views
Welcome To Ask or Share your Answers For Others

1 Answer

Actually, Java doesn't handle this problem at all.

It simply assumes that console encoding is the same as the system default encoding. This assumption is wrong on Windows systems, therefore Java doesn't provide good solution to perform correct console IO with respect to non-ascii characters on Windows.

Possible solutions are:

  • Use System.console() introduced in Java 6:

    BufferedReader in = new BufferedReader(System.console().reader());
    BufferedWriter out = new PrintWriter(System.console().writer(), true);
    
    out.println(in.readLine().toUpperCase());
    

    Note that System.console() can return null when you program run with redirected IO, for example, in IDE. You need a fallback for this case.

  • Specify console encoding explicitly:

    String consoleEncoding = "...";
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in, consoleEncoding));
    BufferedWriter out = new PrintWriter(new OutputStreamWriter(System.in, consoleEncoding), true);
    
    out.println(in.readLine().toUpperCase());
    

    As far as I know, there are no good ways to determine actual console encoding programmatically without native code.

  • Specify console encoding as default encoding using file.encoding property, so that the assumption that console IO uses default encoding would be correct:

    java -Dfile.encoding=... ...
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...