1 // Create a process and pipe data through it. waitFor() the process
2 // in a different thread than the one that created it.
3 import java
.io
.BufferedReader
;
4 import java
.io
.InputStream
;
5 import java
.io
.InputStreamReader
;
6 import java
.io
.OutputStream
;
7 import java
.io
.PrintStream
;
10 public class Process_3
implements Runnable
18 Runtime r
= Runtime
.getRuntime();
19 String
[] a
= { "sed", "-e", "s/Hello/Goodbye/" };
25 OutputStream os
= p
.getOutputStream();
26 PrintStream ps
= new PrintStream(os
);
27 ps
.println("Hello World");
32 System
.out
.println(ex
.toString());
36 public static void main(String
[] args
)
40 Process_3 p3
= new Process_3();
41 Thread t
= new Thread(p3
);
49 InputStream is
= p3
.p
.getInputStream();
50 InputStreamReader isr
= new InputStreamReader(is
);
51 BufferedReader br
= new BufferedReader(isr
);
52 String result
= br
.readLine();
53 if (! "Goodbye World".equals(result
))
55 System
.out
.println("bad 1");
58 result
= br
.readLine();
61 System
.out
.println("bad 2");
64 int c
= p3
.p
.waitFor();
65 System
.out
.println(c
== 0 ?
"ok" : "bad 3");
69 System
.out
.println(ex
.toString());