Scanner vs System.console( ).write( )

后端 未结 2 843
无人共我
无人共我 2021-01-15 16:57

I tried to print out a string that includes Spanish accents on the screen. I get different answer with the same input, but with different approaches.

My first appro

2条回答
  •  孤城傲影
    2021-01-15 17:18

    Not an answer

    You still need to check some things.

    1. The java source encoding used by the java compiler must be the same as the encoding of the text/editor.

      • You can check this with a programmer's editor like JEdit or NotePad++.
    2. Check the default encoding:

      • System.out.println(System.getProperty("file.encoding"));
      • Or add an extra encoding parameter to the Scanner constructor
      • The reason being, that often there is a method/constructor version without encoding where the default platform encoding is taken.
    3. Redirect the System.out/console to a file.

      • In this case it suffices to write the string to UTF-8.
    4. Try the real console, not that of the IDE.

      • The encoding of the IDE "console" may be configurable.

    I find the behaviour puzzling. It would look as if the Scanner took a charset encoding UTF-8 and then had to write it to US-ASCII, so umappable characters are given by . That cannot be. It looks like Scanner would be "buggy" - which I doubt.

    Try dump the string:

    for (int i = 0; i < s.length(); ) {
        int cp = s.codePointAt(i);
        System.out.printf(" %x", cp);
        i += Character.charCount(cp);
    }
    System.out.println();
    

    (Normally not done)

    System.setProperty("file.encoding","Windows-1252");
    

提交回复
热议问题