2015-12-02 45 views
2

有多種方式可以在多線程時將整個控制檯輸出保存到文件中嗎?我正在處理5個線程。我有這個想法,我可以在運行方法中放置printstream。使用多線程控制檯輸出到文件

例如:這裏

public void run() { 
try{ 
    PrintStream out = new PrintStream(file); 
    stopExecute stop = new stopExecute(); 
    Thread t = new Thread(stop); 
    Scanner in = new Scanner(System.in); 
    t.start(); 

     while (!in.hasNextLine()) 
    { 
     classThatUsingMultipleThrads(); 
     System.out.println("Finished"); 
     anotherClassThatUsingThreads(); 
     System.out.println("Finished"); 
    } 
     System.out.prinln("User stopped the execution"); 
     stop.keepRunning = false; 
     System.setOut(out); 

     } 
    catch(IOException e){System.out.println(e);} 

問題是,它的唯一可取「用戶採空執行」在while循環,一切都不會被保存輸出。或者來自其他類的輸出流。

我試圖把

System.setOut(out); 
在while循環

,但並沒有幫助。

編輯:拼寫校正

+0

「Finished」,而不是「Finnished」:)哦。並「停止」而不是「停止」。 –

+0

會使用像Log4J這樣的日誌框架來幫助你的情況嗎? –

+0

@ C-Otto感謝您的糾正,我每天都在學習新事物:) – Micropop

回答

0
try { 
    System.setOut(new PrintStream(new File("output-file.txt"))); 
} 
catch (Exception e) { 
    e.printStackTrace(); 
} 

感謝:System.out to a file in java

0

你或許應該考慮使用一個日誌庫(如log4j)。不過,您也可以使用類似TeeOutputStream的東西。這種類型的輸出流在被調用時寫入2個其他流。一些圖書館有很好的實施,但你也可以自己寫一個。我真正快速地鞭打了這一個。

你可以在你的main的方法來使用這個TeePrintStream,然後System.out.*所有呼叫都將數據寫入到平時System.out和你FileOutputStream爲您的整個程序的輸出流。

也Theres的TeePrintStream的實現,這裏http://www.java2s.com/Code/Java/File-Input-Output/TeePrintStreamteesallPrintStreamoperationsintoafileratherliketheUNIXtee1command.htm

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.OutputStream; 
import java.io.PrintStream; 

public class SO34042494 { 
    public static void main(String[] args) throws FileNotFoundException { 
     System.setOut(new TeePrintStream(System.out, new FileOutputStream(new File("x:\\output.txt")))); 

     System.out.println("Check check"); 
     System.out.println("1"); 
     System.out.println(2); 
     System.out.println(3L); 
    } 

    public static class TeePrintStream extends PrintStream { 
     private final OutputStream tee; 

     public TeePrintStream(PrintStream original, OutputStream tee) { 
      super(original); 
      this.tee = tee; 
     } 

     @Override 
     public void write(byte[] b) throws IOException { 
      super.write(b); 
      tee.write(b); 
     } 

     @Override 
     public void write(byte[] buf, int off, int len) { 
      super.write(buf, off, len); 
      try { 
       tee.write(buf, off, len); 
      } catch (IOException e) { 
       throw new RuntimeException(e); 
      } 
     } 

     @Override 
     public void write(int b) { 
      super.write(b); 
      try { 
       tee.write(b); 
      } catch (IOException e) { 
       throw new RuntimeException(e); 
      } 
     } 

     @Override 
     public synchronized void close() { 
      try { 
       tee.close(); 
      } catch (IOException e) { 
       throw new RuntimeException(e); 
      } finally { 
       super.close(); 
      } 
     } 
    } 
} 

的TeePrintStream我這裏是我剛纔扔在一起,請如果你要在一個生產項目拋光使用這個它,測試它徹底

0

奧基我認爲我解決了它。在我主我只是做了這樣的:

public static void main(String[] args) { 
Prinstream out = new Prinststream(file); 

/* 
    Do some things like start threads, call objects etc.. 
    . 
    . 
    . 
    . 
*/ 
    System.setOut(out); 

我認爲,當所有線程開始,做自己的東西(我分配一個對象到每個線程)的爲PrintStream將捕獲發生在其他每個控制檯輸出類。 編輯我的stopExecute類之前,這沒有奏效。

public class stopExecute implements Runnable { 
    Scanner scan = new Scanner(System.in);  
    private Object[] obj; 

public stopExecute(Object[] obj) 
{ 
    this.obj = obj; 
} 

public void run() { 
    while(true){ 
     stop(); 
    } 
} 
public void stop() { 
    if(scan.hasNextLine()){ 
     System.out.println("Stopped"); 
     System.exit(0); 
    } 
    } 
} 

謝謝你的幫助球員。我會研究你的建議並嘗試它們。在這個解決方案中,我無法使用Log4J。但我一定會檢查出來。