Netbeans Java控制台编码UTF-8和Umlauts [英] Netbeans Java Console Encoding UTF-8 and Umlauts

查看:214
本文介绍了Netbeans Java控制台编码UTF-8和Umlauts的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是关于一个使用NetBeans 7.4编写的Java程序.显然存在编码问题,因为我需要处理包含特殊字符(äüöÄÜÖß)的德语输入.

从文件中读取文本就像一个超级按钮一样,特殊字符被保存并按预期显示:

String fileText = new Scanner(file, "UTF-8" ).useDelimiter("\\A").next();

但是,我还需要从控制台读取用户输入-在这种情况下,我只关心NetBeas本身的输入,因为该代码不会在IDE外部使用.在此处输入特殊字符会导致使用普通符号(方框,问号),而不是变音符号.

Scanner scanner = new Scanner(System.in, "UTF-8");
userQuery = scanner.nextLine();

Input: könig
Output: k�nig

我已经在这个问题上停留了很长一段时间,尝试了Google带来的每一种选择,但到目前为止还算不上什么.大多数人似乎已经通过更改标准编码(项目属性->源->编码)来解决此问题,尽管该编码已经设置为UTF-8.

以任何其他方式使用这些字符都没有问题,例如将其保存为字符串或将其打印到控制台.因此,问题似乎出在NetBeans控制台编码设置上.

我尝试手动进行更改,但没有任何运气.我不确定此设置是否还会影响NetBeans控制台,因为尝试访问控制台对象只会返回null.

System.setProperty("console.encoding", "UTF-8");

任何人都知道下一步该怎么做吗?我已经用尽了所有Google搜索的内容(与以往一样,在第5页上的搜索没有太大用处).

谢谢!

解决方案

多年来,Netbeans控制台窗口中的I/O编码也使我感到困惑,并终于找到原因.

至少在我的系统(Windows 10上为Netbeans 8.1)上,Netbeans控制台令人困惑地将 UTF-8用于输出(这就是为什么您的输出适用于UTF-8输入文件的原因),但是使用了 Windows-1252进行输入. (对于 POLA 来说是如此:)

因此,如果您将扫描仪更改为使用该编码方式

Scanner scanner = new Scanner(System.in, "Windows-1252");

一切正常.或者您可以通过添加

,告诉Netbeans使用UTF-8作为控制台输入编码

-J-Dfile.encoding=UTF-8

etc/netbeans.conf(在Netbeans安装目录中)中的变量netbeans_default_options.

为了从系统命令行运行该应用程序具有最大的一致性,我宁愿使用Windows-1252(或更确切地说是IBM850)作为Windows上的Netbeans控制台编码.但是Netbeans似乎忽略了给定的控制台输出开关,它始终使用UTF-8,所以这是我们能做的最好的事情.

我真的很喜欢Netbeans,但是我希望他们能清理这个烂摊子...

My problem ist about a little java program written using NetBeans 7.4. There is obviously an encoding issue since I need to handle German input containing special characters (äüöÄÜÖß).

Reading in text from files works like a charm, special characters are saved and displayed as expected:

String fileText = new Scanner(file, "UTF-8" ).useDelimiter("\\A").next();

However I also need to read the user input from console - in this case I only care about the one in NetBeas itself since this code will not be used outside the IDE. Entering special characters here leads to the usual symbols (box, question mark) instead of the umlauts.

Scanner scanner = new Scanner(System.in, "UTF-8");
userQuery = scanner.nextLine();

Input: könig
Output: k�nig

I have been stuck on this for quite a while now, having tried every option Google brought my way, but so far no luck. Most people seem to have fixed this by changing the standard encoding (Project Properties -> Sources -> Encoding), which is already set to UTF-8 though.

There is no issue using those characters in any other way, such as saving them in strings or printing them to the console. So the issue seems to be with the NetBeans console encoding setting.

I tried manually changing that without any luck. I'm not sure this setting even affects the NetBeans console, since trying to access the console object just returns null.

System.setProperty("console.encoding", "UTF-8");

Anybody have an idea where to look next? I have already exhausted all Google searches (not much useful on pages > 5, as always).

Thanks!

解决方案

I have also been confused by I/O encoding in the Netbeans console window for years, and have finally found out why.

At least on my system (Netbeans 8.1 on Windows 10), the Netbeans console confusingly uses UTF-8 for output (that's why your output works for UTF-8 input files), but uses Windows-1252 for input. (So much for POLA :)

So if you change your scanner to use that encoding

Scanner scanner = new Scanner(System.in, "Windows-1252");

everything should work fine. Or you can tell Netbeans to use UTF-8 as console input encoding by adding

-J-Dfile.encoding=UTF-8

to the variable netbeans_default_options in etc/netbeans.conf (in Netbeans installation directory).

For maximum consistency with running the app from the system command line, I would have preferred to use Windows-1252 (or rather IBM850) as Netbeans console encoding on Windows. But Netbeans seems to ignore the given switch for the console output, it always uses UTF-8, so that is the best we can do.

I really like Netbeans, but I'd wish they would clean up this mess...

这篇关于Netbeans Java控制台编码UTF-8和Umlauts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆