我有一个 prolog 文件(专家系统),我使用 Jpl 库(org.jpl7.*)从 Java 中查阅,我有一个 UI,我想在其中显示 prolog 查询的输出。这是我的自定义输出流,应该将每个控制台内容重定向到我的界面(jTextAreaOUTPUT 是我重定向内容的地方)
public class CustomOutputStream extends OutputStream {
private JTextArea jTextAreaOUTPUT;
public CustomOutputStream(JTextArea textArea) {
jTextAreaOUTPUT = textArea;
}
@Override
public void write(int b) throws IOException {
// redirects data to the text area
jTextAreaOUTPUT.append(String.valueOf((char)b));
// scrolls the text area to the end of data
jTextAreaOUTPUT.setCaretPosition(jTextAreaOUTPUT.getDocument().getLength());
}
}
这是我的接口类中的一些行:这调用了自定义输出流方法:
PrintStream printStream = new PrintStream(new CustomOutputStream(jTextAreaOUTPUT), true, "UTF-8");
// keeps reference of standard output stream
PrintStream standardOut = System.out;
System.setOut(printStream);
System.setErr(printStream);
由于某些奇怪的原因,它不适用于这个 prolog 文件(我尝试使用其他文件并且它有效):UI 冻结并且内容一直显示在 java 控制台(eclipse)中。专家系统文件与write
Prolog 中的指令一起使用(例如write('Lorem Ipsum')
)
- 为什么standardOut中从未使用过?可以这样声明吗?
- 有没有办法强制重定向应该在 Eclipse 控制台中编写的所有文本?
我还尝试在 prolog 中使用“write Stream”方法,但是(仅针对此 prolog 文件,可能是由于递归)即使 outpus 写入 txt 文件,UI 也会冻结。