Java 控制台无法正确读取中文字符

标签 java eclipse character-encoding

我正在努力让 Eclipse 正确读取中文字符,但我不确定我可能哪里出错了。

具体来说,在从控制台读取一串中文(简体或繁体)和输出之间的某个地方,它会出现乱码。 即使在输出一大串混合文本(英文/中文字符)时,它似乎也只是改变了中文字符的外观。

我已将其缩减为以下测试示例,并用我认为在每个阶段发生的事情对其进行了明确注释 - 请注意,我是一名学生,非常想确认我的理解(或其他):)

public static void main(String[] args) {    
    try 
    {
        boolean isRunning = true;

        //Raw flow of input data from the console
        InputStream inputStream = System.in;
        //Allows you to read the stream, using either the default character encoding, else the specified encoding;
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
        //Adds functionality for converting the stream being read in, into Strings(?)
        BufferedReader input_BufferedReader = new BufferedReader(inputStreamReader);


        //Raw flow of outputdata to the console
        OutputStream outputStream = System.out;
        //Write a stream, from a given bit of text
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8");
        //Adds functionality to the base ability to write to a stream
        BufferedWriter output_BufferedWriter = new BufferedWriter(outputStreamWriter);



        while(isRunning) {
            System.out.println();//force extra newline
            System.out.print("> ");

            //To read in a line of text (as a String):
            String userInput_asString = input_BufferedReader.readLine();

            //To output a line of text:
            String outputToUser_fromString_englishFromCode = "foo"; //outputs correctly
            output_BufferedWriter.write(outputToUser_fromString_englishFromCode);
            output_BufferedWriter.flush();

            System.out.println();//force extra newline

            String outputToUser_fromString_ChineseFromCode = "之謂甚"; //outputs correctly
            output_BufferedWriter.write(outputToUser_fromString_ChineseFromCode);
            output_BufferedWriter.flush();

            System.out.println();//force extra newline

            String outputToUser_fromString_userSupplied = userInput_asString; //outputs correctly when given English text, garbled when given Chinese text
            output_BufferedWriter.write(outputToUser_fromString_userSupplied);
            output_BufferedWriter.flush();

            System.out.println();//force extra newline

        }
    }
    catch (Exception e) {
        // TODO: handle exception
    }
}

示例输出:

> 之謂甚
foo
之謂甚
之謂甚

> oaea
foo
之謂甚
oaea

> mixed input - English: fubar; Chinese: 之謂甚;
foo
之謂甚
mixed input - English: fubar; Chinese: 之謂甚;

> 

在这篇 Stack Overflow 帖子中看到的内容与我在 Eclipse 控制台中看到的内容以及在 Eclipse 调试器中看到的内容完全匹配(查看/编辑变量值时)。通过 Eclipse 调试器手动更改变量值会导致代码根据该值按照我通常期望的方式运行,这表明文本是如何读取 IN 的,这是一个问题。

我已经尝试了许多不同的扫描仪/缓冲流 [reader|writer] 等组合来读入和输出,有和没有明确的字符类型,尽管这不是特别系统地完成并且很容易遗漏一些东西。

我已尝试将 Eclipse 环境设置为尽可能使用 UTF-8,但我想我可能遗漏了一两个地方。请注意,控制台将正确输出硬编码的中文字符。

非常感谢有关此事的任何帮助/指导:)

最佳答案

看起来控制台没有正确读取输入。我认为这是一个链接,它描述了您的问题和解决方法。

http://paranoid-engineering.blogspot.com/2008/05/getting-unicode-output-in-eclipse.html

简单的答案: 尝试在 eclipse.ini 中设置环境变量 -Dfile.encoding=UTF-8。 (在为整个 Eclipse 启用它之前,您可以尝试在该程序的调试配置中设置它,看看它是否有效)

该链接有更多建议

关于Java 控制台无法正确读取中文字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13882378/

相关文章:

php - 页面不显示特殊字符

c - 无效字符测试

nginx - 非拉丁 HTTP 基本身份验证消息编码/NGINX

java - 扩展嵌套在实现接口(interface)的非基类中的非静态类

java - “Main method not found in class…”但它在那里?那为什么会出错呢?

java - 排序和过滤在 Google 应用引擎中不起作用

eclipse - 仅当类与类文件同名时才会出现错误 "self constructor arguments cannot reference unconstructed this"

java - `javax.inject` 在 Spring Boot 中可用吗?

java - 如何在不阻塞的情况下接收来自控制台的输入?

android - 无法解析 Artifact/缺失 Artifact (Eclipse)