代码之家  ›  专栏  ›  技术社区  ›  yan.kun

无法用java将德语“umlauts”(ü)从控制台写入文本文件

  •  2
  • yan.kun  · 技术社区  · 14 年前

    以下是设置扫描仪的代码:

    Scanner scanner = new Scanner(System.in, "UTF8");
    

    String s = scanner.nextLine();
    

    下面是要写入文件的代码:

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

    3 回复  |  直到 14 年前
        1
  •  3
  •   Grodriguez    14 年前

    您的控制台可能不是UTF-8,所以当您这样做时 new Scanner(System.in, "UTF8"); 您正在创建一个编码错误的扫描器,当您尝试从控制台读取行时,您的umlaut将丢失。

    您可能想使用 chcp

    new Scanner(System.in) ,应使用默认的平台编码。

        2
  •  2
  •   MyName    13 年前

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

    Scanner keyboardReader = new Scanner(System.in, "iso-8859-1");
    
        3
  •  0
  •   Leigh josh poley    9 年前

    这对我很有用,德国元音:

    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();
        }
    }