windows控制台中的西里尔文(java) System.out.println();
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10143998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cyrillic in windows Console(java) System.out.println();
提问by yeah its me
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.
当我写一些西里尔文本时,System.out.println("Русский язык") - 然后它输出这个╨?ёёъшщ ?ч√ъ,使用windows控制台,如何修复?,文件编码为utf-8,但没关系,当它是 ansii 或 windows-1251 时,它的输出相同。
回答by Lumi
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.outuses the platform encoding (cp1252 for me), and that doesn't have cyrillic characters.
因此,您可以使用PrintStream使其工作。该System.out中使用该平台的编码(CP1252对我来说),而且没有西里尔字母。
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 Unicodein the casual sense that it can print Greek and Russian.
因此,您可以看到,与某些人的看法相反,Windows 控制台确实可以在随意的意义上理解Unicode,它可以打印希腊语和俄语。
回答by dragon66
Although you can switch Windows console to UTF-8 by chcp 65001, you may still not be able to view UTF-8 output properly. This may not be what you want, but it at least is a choice: redirect your standard output to a file. Save your source file as UTF-8 and compile it using UTF-8 encoding. The redirected output file can be viewed with a UTF-8 aware text editor.
尽管您可以通过 chcp 65001 将 Windows 控制台切换到 UTF-8,但您可能仍然无法正确查看 UTF-8 输出。这可能不是您想要的,但至少是一个选择:将您的标准输出重定向到一个文件。将您的源文件另存为 UTF-8 并使用 UTF-8 编码对其进行编译。可以使用支持 UTF-8 的文本编辑器查看重定向的输出文件。
String s = "Русский язык";
System.setOut(new PrintStream(new FileOutputStream("out.txt"), true, "UTF-8"));
System.out.println(s);
回答by 9000
Windows console uses encoding CP866 for Cyrillic, for historical reasons (remember DOS?). Windows console is definitely not Unicode-capable.
由于历史原因,Windows 控制台使用 CP866 编码西里尔文(还记得 DOS 吗?)。Windows 控制台绝对不支持 Unicode。
(Alas, I have no Windows machine around to provide a tested code snippet.)
(唉,我周围没有 Windows 机器来提供经过测试的代码片段。)