8

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 つの場所を見逃していた可能性があると思います。コンソールは、ハードコードされた中国語の文字を正しく出力することに注意してください。

この問題に関する支援/ガイダンスは大歓迎です:)

4

3 に答える 3

2

コンソールが入力を正しく読み取っていないようです。これは、問題と回避策を説明していると思われるリンクです。

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

簡単な答え: eclipse.ini で環境変数 -Dfile.encoding=UTF-8 を設定してみてください。(Eclipse全体でこれを有効にする前に、このプログラムのデバッグ構成でこれを設定してみて、動作するかどうかを確認してください)

リンクにはさらに多くの提案があります

于 2013-01-23T19:05:01.457 に答える
1

これを試してください:Eclipseで、メインクラスを右クリックし、[実行]>[構成の実行]をクリックします。次に、[共通]タブに移動し、エンコーディングをUTF-8に変更します。それはうまくいくはずです!

于 2012-12-14T16:20:46.277 に答える
0

これはエンコーディングの問題のようです。ここで2つの問題が発生する可能性があります。1。ASCII文字以外を読み取るコンパイラ機能をアクティブにしていない。この場合、UTF-8文字を読み取ることができる必要があります。2.特定の言語パックを削除した可能性がありますか?あなたはおそらく漢字を書くことができるので、これはありそうにありませんか?

IDEを検索して、非ASCII文字を正しくコンパイルする方法を学ぶ必要があります。Pythonでは、これはコード自体で行われますが、Javaでどのように行われるのかわかりません。

于 2013-01-29T11:36:08.810 に答える