无法写出德语“变音符号”; (äöü) 使用 java 从控制台到文本文件
目前,我正在拼命尝试将从控制台读取的德语变音符号写入 Windows 7 上的 utf8 编码文本文件中。
这是设置扫描仪的代码:
Scanner scanner = new Scanner(System.in, "UTF8");
这是读取字符串的代码:
String s = scanner.nextLine();
这是要写入的代码到文件中:
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(this.targetFile), "UTF8");
osw.write(s);
不幸的是,这样编写的文件不是以 utf8 编码的,而不是示例“überraschung”,但不会显示元音变音。该怎么办?
currently I'm desperately trying to write german umlauts, read from the console, into a utf8 encoded text file on windows 7.
Here is the code to setup the scanner:
Scanner scanner = new Scanner(System.in, "UTF8");
Here is the code to read the string:
String s = scanner.nextLine();
Here is the code to write into a file:
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(this.targetFile), "UTF8");
osw.write(s);
Unfortunately, instead of example "überraschung" the so written file is encoded in utf8 but will not display the umlaut. What to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的控制台可能不是 UTF-8,因此当您执行
new Scanner(System.in, "UTF8");
时,您正在创建一个使用错误编码的扫描仪,并且当您尝试时,您的变音符号会丢失从控制台读取行。您可能需要在控制台提示符下使用 chcp 来检查正在使用的代码页。
事实上,您可能根本不需要指定编码。如果您只是将扫描仪创建为
new Scanner(System.in)
,则应使用默认平台编码。Your console probably is not UTF-8, so when you do
new Scanner(System.in, "UTF8");
you are creating a scanner with the wrong encoding, and your umlauts are lost when you try to read lines from the console.You may want to use
chcp
on a console prompt to check what code page is being used.In fact, you might not need to specify an encoding at all. If you just create the scanner as
new Scanner(System.in)
, the default platform encoding should be used.我遇到了类似的问题(扫描仪不会“检测到”字符串“ç”,而像“Açores”这样的字符串会有 ç 字符“乱码”)。
我通过声明该语言的字符集解决了这个问题:
I had a similar problem (The String "ç" would not be "detected" by the Scanner and Strings like "Açores" would have the ç character "garbled").
I solved it by declaring the charset for the language:
这对我有用,带有德语变音符号:
This worked for me, with german umlauts: