[英]Why does additional characters show up when I print unicode characters to Windows 7 console from a Java program?
Below is a Java program to print a unicode character to windows console 下面是一个Java程序,用于向Windows控制台打印Unicode字符
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
public class PrintUnicodeChar {
public static void main (String[] argv) throws UnsupportedEncodingException {
String unicodeMessage = "\u00A3"; // Pound sign
PrintStream out = new PrintStream(System.out, true, "UTF-8");
out.print(unicodeMessage);
}
}
I have selected Lucida Console as the font and set the codepage to 65001. 我选择了Lucida Console作为字体,并将代码页设置为65001。
The output I get is £ 我得到的输出是£。
If I print the pound sign three times using "\£\£\£"
, the output becomes £££ £. 如果我使用
"\£\£\£"
将磅符号打印三次,输出将变为£££££。 Printing characters with higher unicode value outputs more making it more garbled. 打印具有更高unicode值的字符会输出更多-使其更乱码。
Here is another string "\£\\n\£\£\\n\£\£\£\\n\£\£\£\£\\n\£\£\£\£\£\\n"
这是另一个字符串
"\£\\n\£\£\\n\£\£\£\\n\£\£\£\£\\n\£\£\£\£\£\\n"
The output is 输出是
£ £
££ ££
£££ £££
££££ ££££
£££££ £££££
£ £
£££££ £££££
££ ££
What is happening? 怎么了? Is it a problem with the Windows 7 terminal?
Windows 7终端是否有问题? How to prevent the additional characters from printing?
如何防止其他字符打印?
There is nothing wrong with your code. 您的代码没有错。
The problem occurred, because windows does not support UTF-8 in a console property [ similar problem in python: How to display utf-8 in windows console ] 发生问题是因为Windows在控制台属性中不支持UTF-8 [python中的类似问题: 如何在Windows控制台中显示utf-8 ]
You can bypass this problem by printing to file in the following way: 您可以通过以下方式打印到文件来绕过此问题:
PrintStream out = new PrintStream(new FileOutputStream(fileDir), true, "UTF8");
Full code: 完整代码:
public static void main(String[] args) throws IOException {
String unicodeMessage = "\u00A3"; // Pound sign
File fileDir = new File("c:\\temp\\test.txt");
PrintStream out = new PrintStream(new FileOutputStream(fileDir), true, "UTF-8");
out.print(unicodeMessage);
}
Another solution: Run your code in docker on linux vm (in order to avoid windows related issues) 另一个解决方案:在linux vm上的docker中运行代码(以避免与Windows相关的问题)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.