Java Unicode 字符在 IntelliJ IDEA 控制台中显示为问号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1082343/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:21:22  来源:igfitidea点击:

unicode characters appear as question marks in IntelliJ IDEA console

javaunicodeintellij-ideaconsole

提问by Eran

I'm trying to write unicode characters (?) using System.out, and a question mark gets printed instead.

我正在尝试使用 System.out 编写 unicode 字符 (?),而是打印了一个问号。

How can I have proper unicode characters displayed instead of question marks?

如何显示正确的 unicode 字符而不是问号?

I'm using IntelliJ IDEA on Windows, and trying to print from within the IDE.

我在 Windows 上使用 IntelliJ IDEA,并尝试从 IDE 中进行打印。

采纳答案by Esko Luontola

Is the file encoding configured correctly? See that "Settings | File Encodings" uses UTF-8. Printing ? works for me when I have IDE encoding and all files set to UTF-8. Recompiling may be needed after changing the encoding.

文件编码配置是否正确?请参阅“设置 | 文件编码”使用 UTF-8。印刷 ?当我有 IDE 编码并且所有文件都设置为 UTF-8 时,它对我有用。更改编码后可能需要重新编译。

回答by Thorbj?rn Ravn Andersen

System.out uses the default encoding of the underlying operating system which typically is ISO-8859-1 or UTF-8. The first is what I have with the 1252 codepage under XP.

System.out 使用底层操作系统的默认编码,通常是 ISO-8859-1 或 UTF-8。第一个是我在 XP 下的 1252 代码页。

Is this in a CMD.EXE window, or inside an IDE?

这是在 CMD.EXE 窗口中,还是在 IDE 中?

回答by rhimbo

Do you have the appropriate fonts installed on your machine? The question mark appears when you don't have fonts for the characters you're outputting.

您的机器上是否安装了适当的字体?当您输出的字符没有字体时,会出现问号。

Also, are you outputting by doing a System.out.println("<your string>")? If so, just installing the fonts should work.

另外,您是否通过执行输出System.out.println("<your string>")?如果是这样,只需安装字体即可。

If you are trying to write to System.outfrom within your program, that's different. You have to use an OutputStreamWriter, which is a character stream. You can't just write to a byte-oriented stream such as OutputStream.

如果您尝试System.out从程序内部写入,那就不同了。您必须使用OutputStreamWriter,这是一个字符流。您不能只写入面向字节的流,例如OutputStream.

Look up the API and class reference for OutputStreamWriterand subclasses such as PrintWriter. You construct it giving the locale of the constructor. For example,

查找 API 和类参考OutputStreamWriter以及子类,例如PrintWriter. 你构造它给构造函数的语言环境。例如,

PrintWriter pw = new PrintWriter(System.out, "UTF-8");

PrintWriter pw = new PrintWriter(System.out, "UTF-8");

回答by McDowell

If you ultimately want to print a wide range of Unicode characters on a standard command line on Windows, there is a bit of work involved. The default raster font will not support the characters and applications usually need to call the Unicode console API to render them. Java does not - it will first encode the characters to the native character set (a lossy process) and then emit them using an ANSI call. You can read this blog postif you want the gory details.

如果您最终想在 Windows 的标准命令行上打印范围广泛的 Unicode 字符,则需要做一些工作。默认光栅字体不支持字符,应用程序通常需要调用 Unicode 控制台 API 来呈现它们。Java 没有 - 它首先将字符编码为本地字符集(有损过程),然后使用 ANSI 调用发出它们。如果你想要血腥的细节,你可以阅读这篇博文

回答by Mike Nakis

A little update for the year 2015

2015年的小更新

TL;DR answer:

TL;博士回答:

Go to Settings-> Editor-> File Encodings-> Project Encodingand set it to "UTF-8".

转到Settings-> Editor-> File Encodings->Project Encoding并将其设置为“UTF-8”。

Expanded answer:

扩展答案:

The reason why it does not work can be found by placing a breakpoint on a System.out.print()call. When the breakpoint hits, you can add System.outto Watches, and you can see that System.out.textOut.out.se.csis set to windows-1252or something similarly unsuitable.

不工作的原因可以通过在System.out.print()调用上放置断点来找到。当断点命中时,您可以添加System.outWatches,您可以看到System.out.textOut.out.se.cs设置为windows-1252或类似的不合适的东西。

The setting which magically worked for me (I do not know why) is in Settings-> Editor-> File Encodings-> Project Encoding. You need to set that to "UTF-8".

对我来说神奇的设置(我不知道为什么)在Settings-> Editor-> File Encodings-> 中Project Encoding。您需要将其设置为“UTF-8”。

Then, unicode characters display properly on the console, and one more quick look with the debugger shows that the value of System.out.textOut.out.se.cshas magically turned into UTF-8.

然后,unicode 字符在控制台上正确显示,用调试器再快速查看一下,就会发现 的值System.out.textOut.out.se.cs神奇地变成了UTF-8.

I am saying "magically" because I do not see how and why an editor setting should affect the character set that System.outgets instantiated with when launching/debugging an application. If someone knows what is the logic behind this, please do tell!

我说“神奇”是因为我不明白编辑器设置如何以及为什么会影响System.out启动/调试应用程序时实例化的字符集。如果有人知道这背后的逻辑是什么,请告诉!

回答by Langelihle Kwinda

The settings are not configured correctly, make sure that the your File encodings are set to UTF-8, in particular the 'Default encoding properties files'. On my side the steps below didnt work until I changed the default encoding.

设置未正确配置,请确保您的文件编码设置为 UTF-8,尤其是“默认编码属性文件”。在我这边,在我更改默认编码之前,以下步骤不起作用。

On Intellij 2017.3.1 (Community Edition) File | Settings | Editor > File Encodings . Click on link below to see settings image

关于 Intellij 2017.3.1(社区版)文件 | 设置 | 编辑器 > 文件编码。单击下面的链接查看设置图像

Intellij 2017.3.1 (Community Edition)

Intellij 2017.3.1(社区版)

回答by Kirill Karmazin

  1. Do what @Mike Nakis said in his answer with going to Settings and changing project Encoding.

  2. In my case I had to do an additional step:

  1. 按照@Mike Nakis 在他的回答中所说的去设置和更改项目编码。

  2. 在我的情况下,我必须做一个额外的步骤:

In the bottom right corner of IntelliJ IDEA switch encoding to UTF-8 enter image description here

在 IntelliJ IDEA 右下角切换编码为 UTF-8 在此处输入图片说明

Notethat if you had some text pasted in your class with a different encoding (in my case I pasted a block of Cyrillic text right from the browser) it will become ugly formatted but that's ok. Just remove it and after switching to UTF-8 put it back.

请注意,如果您在类中粘贴了一些使用不同编码的文本(在我的情况下,我直接从浏览器粘贴了一段西里尔文文本),它的格式会变得难看,但没关系。只需将其删除并在切换到 UTF-8 后将其放回原处。

回答by Nicolas

Go to Help> Edit Custom VM options...then add the following option:

转到Help>Edit Custom VM options...然后添加以下选项:

-Dconsole.encoding=UTF-8
-Dfile.encoding=UTF-8

I'm not sure if both are necessary but it worked for me. You need to restart IntelliJ for changes to be applied.

我不确定两者是否都需要,但它对我有用。您需要重新启动 IntelliJ 才能应用更改。

I had already tried changing every encoding setting in Intellij, setting those options in Gradle and changing the system encoding, this is the only one that worked.

我已经尝试更改 Intellij 中的每个编码设置,在 Gradle 中设置这些选项并更改系统编码,这是唯一有效的方法。