เปลี่ยนเส้นทางเนื้อหาคอนโซล Java บน Java UI

ฉันมีไฟล์ prolog (ระบบผู้เชี่ยวชาญ) ที่ฉันปรึกษาจาก Java โดยใช้ไลบรารี Jpl (org.jpl7.*) และฉันมี UI ที่ฉันต้องการแสดงผลลัพธ์ของการสืบค้นของ prolog นี่คือ Custom Output Stream ของฉันที่ควรเปลี่ยนเส้นทางเนื้อหาคอนโซลทั้งหมดไปยังอินเทอร์เฟซของฉัน (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());
 }
}

นี่คือบางบรรทัดที่ฉันมีใน Interface Class ของฉัน: สิ่งนี้เรียกวิธี Custom Output Stream:

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) ไฟล์ Expert System ใช้งานได้กับ write คำสั่งใน Prolog (เช่น write('Lorem Ipsum') )

  1. ทำไม StandardOut ถึงไม่เคยใช้? ประกาศแบบนี้โอเคมั้ย?
  2. มีวิธีบังคับให้เปลี่ยนเส้นทางสำหรับข้อความทั้งหมดที่ควรเขียนในคอนโซล eclipse หรือไม่?

ฉันยังพยายามใช้วิธี "write Stream" ใน prolog แต่ (สำหรับไฟล์ prolog นี้เท่านั้น อาจเกิดจากการเรียกซ้ำ) UI หยุดทำงานแม้ว่าจะเขียน outpus ในไฟล์ txt ก็ตาม


person 5minutsOfBullfight    schedule 05.10.2015    source แหล่งที่มา
comment
คุณอาจต้องแทนที่ฟังก์ชันการเขียนอื่นๆ ใน outputstream write(byte[] b), write(byte[] b, int off, int len) หากผู้เขียนไม่ได้เขียนทีละอักขระ   -  person WillShackleford    schedule 05.10.2015
comment
คุณสามารถทำได้หรือคุณรู้วิธี? ขอบคุณ   -  person 5minutsOfBullfight    schedule 05.10.2015


คำตอบ (1)


คุณอาจต้องแทนที่ฟังก์ชันการเขียนอื่น ๆ ใน outputstream 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);
    }

}
person WillShackleford    schedule 05.10.2015