1

Java Runtime.getRuntime().exec() を使用して、Java で pt_BR スペルチェック用の Linux aspell プログラムを呼び出そうとしています。問題は、入力/出力文字エンコーディングで問題が発生しているように見えることです。一部の入力単語はアクセント文字の位置で 2 つの単語に分割され、出力はアクセント文字の � で表示されます。aspell がコマンド パイプライン モードで pt_BR を正しくスペル チェックできることがわかります。aspell をセットアップするためのコードは次のとおりです。

String[] aspellCommand = new String[4];
            aspellCommand[0] = "aspell";
            aspellCommand[1] = "-a";
            aspellCommand[2] = "--keymapping=ispell";
            aspellCommand[3] = "--lang=pt_BR";
            String[] envArray = new String[0];

            process = Runtime.getRuntime().exec(aspellCommand, envArray);

            System.out.println(Charset.defaultCharset()); /utf-8

            InputStreamReader ir = new InputStreamReader(process.getInputStream(), "utf-8");
            OutputStreamWriter or = new OutputStreamWriter(process.getOutputStream(), "utf-8");

            aspellOutputStream = new BufferedReader(ir);
            aspellInputStream = new PrintWriter(or,true);

            System.out.println(ir.getEncoding()); /utf-8
            System.out.println(or.getEncoding()); /utf-8

入力を aspell にフィードするコード:

aspellInputStream.println(misSpelledWord);

aspell から読み取るコード:

String line = aspellOutputStream.readLine();

IO ストリームの文字セットのエンコーディングに問題があると思われますが、正確に設定する場所がわかりません。

4

1 に答える 1

1

バグのある Runtume.exec を使用しないでください。代わりに ProcessBuilder を使用してください。またはhttps://github.com/zeroturnaround/zt-exec

于 2015-01-14T02:52:32.607 に答える