我正在尝试使用 Java Runtime.getRuntime().exec() 调用 linux aspell 程序以在 Java 中进行 pt_BR 拼写检查。问题是输入/输出字符编码似乎有问题。某些输入单词在重音字母位置被拆分为两个单词,输出显示为带有重音字母的�。我可以看到 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 流字符集编码有问题,但我不知道在哪里设置它们。