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: 之謂甚;
>
このスタック オーバーフローの投稿に表示される内容は、Eclipse コンソールに表示される内容と Eclipse デバッガー内で表示される内容 (変数値を表示/編集する場合) と正確に一致します。Eclipse デバッガーを介して変数値を手動で変更すると、その値に応じてコードが通常どおりに動作するようになり、テキストがどのように読み取られるかが問題であることを示唆しています。
スキャナ/バッファリングされたストリーム[リーダー|ライター]などのさまざまな組み合わせを試して、明示的な文字タイプの有無にかかわらず、読み取りと出力を行いましたが、これは特に体系的に行われたわけではなく、何かを見逃す可能性がありました.
可能な限り UTF-8 を使用するように Eclipse 環境を設定しようとしましたが、1 つか 2 つの場所を見逃していた可能性があると思います。コンソールは、ハードコードされた中国語の文字を正しく出力することに注意してください。
この問題に関する支援/ガイダンスは大歓迎です:)