ฉันมีไฟล์ 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')
)
- ทำไม StandardOut ถึงไม่เคยใช้? ประกาศแบบนี้โอเคมั้ย?
- มีวิธีบังคับให้เปลี่ยนเส้นทางสำหรับข้อความทั้งหมดที่ควรเขียนในคอนโซล eclipse หรือไม่?
ฉันยังพยายามใช้วิธี "write Stream" ใน prolog แต่ (สำหรับไฟล์ prolog นี้เท่านั้น อาจเกิดจากการเรียกซ้ำ) UI หยุดทำงานแม้ว่าจะเขียน outpus ในไฟล์ txt ก็ตาม