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

我正在努力让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 Overflowpost上看到的内容完全符合我在Eclipse控制台中看到的以及在Eclipse调试器中看到的内容(查看/编辑变量值时)。 通过Eclipse调试器手动更改变量值会导致代码依赖于该值的行为,就像我通常期望的那样,这表明文本读取IN的方式是一个问题。

我已经尝试过许多不同的扫描仪/缓冲流[读写器]等的组合来读入和输出,有和没有明确的字符类型,虽然这没有特别系统地完成,很容易错过一些东西。

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

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

看起来控制台没有正确读取输入。 这是一个我相信描述你的问题和工作轮次的链接。

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

简单回答:尝试在eclipse.ini中设置环境变量-Dfile.encoding = UTF-8。 (在为整个eclipse启用此function之前,您可以尝试在此程序的debug configurtion中设置此项并查看它是否有效)

该链接有更多的建议

试试这个:在eclipse中,右键单击主类,然后单击run as> run configurations。 然后转到common选项卡并将编码更改为UTF-8。 这应该工作!

这似乎是一个编码问题。 这里可能存在两个问题:1。您没有激活编译器读取除ASCII字符之外的任何内容的能力,在您的情况下,您需要能够读取UTF-8字符。 2.您可能删除了某些语言包? 这不太可能,因为你可能会写汉字?

您应该四处搜索并了解IDE如何正确编译非ASCII字符。 在python中,这是在代码本身完成的,我不确定它是如何在Java中完成的。