Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
716 views
in Technique[技术] by (71.8m points)

encoding - cyrillic in windows Console(java) System.out.println();

When i write some cyrillic text, System.out.println("Русский язык") - then it outpus this ╨?ёёъшщ ?ч√ъ, using windows console, how can be this fixed?, the file encoding is utf-8, but it doesn't matter, when it was ansii or windows-1251, it were outputing the same.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)
import java.io.PrintStream;
class Kyrill {
    public static void main(String args[])
        throws java.io.UnsupportedEncodingException
    {
        String ru = "Русский язык";
        PrintStream ps = new PrintStream(System.out, true, "UTF-8");
        System.out.println(ru.length());
        System.out.println(ru);
        ps.println(ru);
    }
}

D:Temp :: chcp 65001
Aktive Codepage: 65001.

D:Temp :: javac -encoding utf-8 Kyrill.java && java Kyrill
12
??????? ????
Русский языкй язык

Note that you might see some trailing junk in the output (I do) but if you redirect the output to a file you'll see that this is just a display artefact.

So you can make it work by using a PrintStream. The System.out uses the platform encoding (cp1252 for me), and that doesn't have cyrillic characters.

Additional note for you to grok the encoding business:

D:Temp :: chcp 1251
Aktive Codepage: 1251.
:: This is another codepage (8 bits only) that maps bytes to cyrillic characters.
:: Edit the source file to have:
::      PrintStream ps = new PrintStream(System.out, true, "Windows-1251");
:: We intend to match the console output; else we won't get the expected result.
D:Temp :: javac -encoding utf-8 Kyrill.java && java Kyrill
12
??????? ????
Русский язык

So you can see that contrary to what some people believe, the Windows console does grok Unicode in the casual sense that it can print Greek and Russian.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...