2015-12-02 59 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。但我一定会检查出来。