我想写一个程序,重定向System.out到JTextArea(它不一定是一个JTextArea),但是当我打电话System.out.println(“ !测试“)输出到文本区是像这样:Java - 与自定义OutputStream PrintStream奇怪的行为
\n
st!
\n
我的OutputStream代码:
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);
任何人都可以告诉我地球上发生了什么?
这代码在这里显示编译错误。为了更快地获得更好的帮助,请发布[SSCCE](http://sscce.org/)。 – 2013-04-20 08:15:51
如何调用写入函数?它有什么论点? – Zyerah 2013-04-20 08:16:11
“\ n”是逐字显示的吗? 'getDocument()'返回的类型是什么,以及它是如何处理换行符的? – 2013-04-20 08:18:28