0

我有一个 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)中。专家系统文件与writeProlog 中的指令一起使用(例如write('Lorem Ipsum')

  1. 为什么standardOut中从未使用过?可以这样声明吗?
  2. 有没有办法强制重定向应该在 Eclipse 控制台中编写的所有文本?

我还尝试在 prolog 中使用“write Stream”方法,但是(仅针对此 prolog 文件,可能是由于递归)即使 outpus 写入 txt 文件,UI 也会冻结。

4

1 回答 1

0

如果编写器一次不写入一个字符,您可能需要覆盖输出流中的其他写入函数 write(byte[] b), write(byte[] b, int off, int len)

要覆盖 OutputStream 的其他写入函数,只需提供与您已经编写的单字符函数类似的代码:

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());
    }

    @Override
    public void write(byte[] b, int off, int len) throws IOException {
        // redirects data to the text area
        jTextAreaOUTPUT.append(new String(b, off, len));
        // scrolls the text area to the end of data
        jTextAreaOUTPUT.setCaretPosition(jTextAreaOUTPUT.getDocument().getLength());
    }

    @Override
    public void write(byte[] b) throws IOException {
        write(b,0,b.length);
    }

}
于 2015-10-05T12:06:52.380 回答