2014-02-10 23 views
1

我使用使用基於Eclipse的Java做的Hadoop數據分析,我讓我的日誌輸出如圖所示日食下面的圖片中,我們怎麼可以把它重定向到一個文本區域..我重定向輸出到textarea的

Screen shot

+0

看看這裏http://stackoverflow.com/questions/2851234/system-out-to-a-file-in-java –

回答

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)); 
} 
+0

我不給他們輸出他們自動生成,所以它不幫助 – vakul

+1

其只打印數據,即由我在system.out.print()中給出的我想要打印日誌文件.. – vakul

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)

+0

沒有它沒有加載到該文件我試過.. – vakul