无法写出德语“变音符号”; (äöü) 使用 java 从控制台到文本文件

发布于 2024-09-26 03:02:57 字数 465 浏览 9 评论 0原文

目前,我正在拼命尝试将从控制台读取的德语变音符号写入 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

等往事风中吹 2024-10-03 03:02:58

您的控制台可能不是 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.

暖树树初阳… 2024-10-03 03:02:58

我遇到了类似的问题(扫描仪不会“检测到”字符串“ç”,而像“Açores”这样的字符串会有 ç 字符“乱码”)。

我通过声明该语言的字符集解决了这个问题:

Scanner keyboardReader = new Scanner(System.in, "iso-8859-1");

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:

Scanner keyboardReader = new Scanner(System.in, "iso-8859-1");
山有枢 2024-10-03 03:02:58

这对我有用,带有德语变音符号:

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;


public class P {

    public static void main(String[] args) throws Exception {
        BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
        String s = stdin.readLine();
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:/p.txt"), "UTF-8");
        osw.write(s);
        osw.close();
    }
}

This worked for me, with german umlauts:

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;


public class P {

    public static void main(String[] args) throws Exception {
        BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
        String s = stdin.readLine();
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:/p.txt"), "UTF-8");
        osw.write(s);
        osw.close();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文