当我写一些西里尔文文本时,System.out.println("Русский язык") - 然后它输出这个╨єёёъшщ ч√ъ,使用Windows控制台,如何解决这个问题?,文件编码是utf-8 ,但没关系,当它是ansii或windows-1251时,它的输出是相同的。
最佳答案
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
??????? ????
Русский языкй язык
请注意,您可能会在输出中看到一些尾随垃圾(我确实如此),但如果您将输出重定向到文件,您会发现这只是一个显示伪影。
所以你可以通过使用 PrintStream 来使其工作。 System.out使用平台编码(对我来说是 cp1252),并且没有西里尔字符。
关于编码业务的补充说明:
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
??????? ????
Русский язык
因此您可以看到,与某些人的看法相反,Windows 控制台确实支持 Unicode,它可以打印希腊语和俄语。
关于java - Windows 中的西里尔文 Console(java) System.out.println();,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10143998/