我正在尝试编写一个将 System.out 重定向到 JTextArea 的程序(它不必是 JTextArea),但是当我调用 System.out.println("Test!") 时,文本区域的输出就像所以:
\n
st!
\n
我的输出流的代码:
package gui;
import java.awt.*;
import java.io.*;
import javax.swing.text.*;
public class LogOutputStream extends OutputStream
{
public void write(final int b) throws IOException
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
write0(b);
}
});
}
public void write(final byte[] b, final int off, final int len)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
write0(b, off, len);
}
});
}
public void write(final byte[] b)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
write0(b);
}
});
}
private void write0(int b)
{
Document doc = FernflowerGUI.frame.textArea.getDocument();
try
{
doc.insertString(doc.getLength(), String.valueOf((char)b), null);
}
catch(BadLocationException impossible)
{
}
}
private void write0(byte[] b, int off, int len)
{
Document doc = FernflowerGUI.frame.textArea.getDocument();
try
{
doc.insertString(doc.getLength(), new String(b, off, len), null);
}
catch(BadLocationException impossible)
{
}
}
private void write0(byte[] b)
{
write0(b, 0, b.length);
}
}
创建 PrintStream 的代码:
PrintStream ps = new PrintStream(new LogOutputStream(), true);
谁能告诉我地球上发生了什么?