简体   繁体   English

从Java程序将Unicode字符打印到Windows 7控制台时,为什么还会显示其他字符?

[英]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.

相关问题 当我使用 BufferedReader 从控制台读取字符并打印它们时,为什么会打印空行? - Why empty lines are being printed at the when I use BufferedReader to read characters from the console and print them? 为什么 Windows 控制台代码页会影响带有 ANSI 字符的 Java 程序的编译和运行? - Why Windows console code page influences the compilation and running of a Java program with ANSI characters? Windows控制台中的unicode字符比预期的多 - More unicode characters in windows console than expected Netbeans 控制台不显示孟加拉语 unicode 字符 - Netbeans console does not display Bangla unicode characters Unicode特殊字符出现在Java控制台中,但没有出现在Swing中 - Unicode special characters appearing in Java console, but not in Swing VSCode Java UTF-8 不打印字符到控制台 - VSCode Java UTF-8 does not print characters to the console 为什么Java允许在源代码中转义unicode字符? - Why does Java permit escaped unicode characters in the source code? 从Flex到Java的Unicode字符 - Unicode characters from Flex to Java 用Java打印前1000个Unicode字符 - Print the first 1000 Unicode characters in Java 为什么 Java 到 HTML 文件中的字符串会出现额外的字符? - Why additional characters appear when String in Java to HTML file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM