我正在使用eclipse上的java进行数据分析,我得到了我的输出日志,如下图中的eclipse所示,我们如何将它重定向到文本区域..
答案 0 :(得分:0)
试试这个:
public class JTextAreaOutputStream extends OutputStream {
private JTextArea destination;
public JTextAreaOutputStream(JTextArea destination) {
if (destination == null)
throw new IllegalArgumentException ("Destination is null");
this.destination = destination;
}
@Override
public void write(int b) throws IOException {
write (new byte [] {(byte)b}, 0, 1);
}
@Override
public void write(byte[] buffer, int offset, int length) throws IOException
{
final String text = new String (buffer, offset, length);
SwingUtilities.invokeLater(new Runnable ()
{
@Override
public void run()
{
destination.append (text);
}
});
}
}
然后像这样重定向文本区域:
public void setSystemOutRedirect() {
JTextAreaOutputStream out = new JTextAreaOutputStream (textAreaUpgraderLog);
System.setOut (new PrintStream(out));
System.setErr(new PrintStream(out));
}
答案 1 :(得分:0)
只需在程序开头使用以下代码段即可。
File file = new File("D:/out.txt");
FileOutputStream fos = new FileOutputStream(file);
PrintStream ps = new PrintStream(fos);
System.setErr(ps);
然后所有标准错误都将被重定向到D:/out.txt
文件。
要重定向标准输出,请使用System.setOut(ps)
答案 2 :(得分:0)
这些主要是反击和进步所以,看看 this post 和JobClient's API。
另外,我认为你会发现this post非常有用。