KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > trading > test > OutputForwarder


1 package org.jacorb.trading.test;
2
3 import java.io.*;
4 /**
5  * This class writes the data of a processes output stream to a file.
6  * This is needed since when using exec() the new process has no stdout.
7  *
8  * @author Nicolas Noffke
9  */

10
11 public class OutputForwarder extends Thread JavaDoc {
12     private File m_out = null;
13     private Process JavaDoc m_process = null;
14     
15     public OutputForwarder (Process JavaDoc proc, String JavaDoc filename){
16     m_process = proc;
17     m_out = new File(filename);
18
19     start();
20     }
21     
22     public void run(){
23     BufferedReader _in = new BufferedReader(new InputStreamReader(m_process.getInputStream()));
24     String JavaDoc _line = null;
25     PrintWriter _out = null;
26     
27     try{
28          _out = new PrintWriter(new FileWriter(m_out));
29
30         // If we get null from readLine() we assume that the process has exited.
31
// Unfortunately there is no exception thrown when trying to read from
32
// a dead processes output stream.
33
while((_line = _in.readLine()) != null)
34         _out.println(_line);
35
36     }catch (Exception JavaDoc _e){
37         _e.printStackTrace();
38     }
39     finally{
40         try{
41         _in.close();
42         _out.flush();
43         _out.close();
44         }catch (Exception JavaDoc _e){
45         _e.printStackTrace();
46         }
47     }
48     System.out.println("A Trader has exited");
49     
50     }
51 } // OutputForwarder
52

53
54
55
56
57
58
59
60
61
62
63
64
Popular Tags