没有使用java从控制台写入德语“umlauts”(äöü)到文本文件 [英] Failing to write german 'umlauts' (äöü) from console to text file with java

查看:359
本文介绍了没有使用java从控制台写入德语“umlauts”(äöü)到文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我拼命想把德语umlaut从控制台中读入Windows 7上的utf8编码文本文件。



这里是设置代码扫描仪:

 扫描仪扫描仪= new Scanner(System.in,UTF8); 

这是读取字符串的代码:

  String s = scanner.nextLine(); 

这是写入文件的代码:

  OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(this.targetFile),UTF8); 

osw.write(s);不幸的是,代替例子überraschung,这样写的文件被编码在utf8中,但是不会显示出来。 umlaut。

$ p

解决方案

您的控制台可能不是UTF-8,所以当您执行 new Scanner (System.in,UTF8); 您正在使用错误的编码创建扫描仪,并且当您尝试从控制台读取行时您的变音符号会丢失。



您可能想在控制台提示符下使用 chcp 来检查正在使用的代码页。



事实上,您可能不需要指定编码。如果您只是创建扫描仪 new Scanner(System.in),则应使用默认的平台编码。


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?

解决方案

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.

这篇关于没有使用java从控制台写入德语“umlauts”(äöü)到文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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