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());
37 public static void main(String
[] args
)
41 Process_3 p3
= new Process_3();
42 Thread t
= new Thread(p3
);
50 InputStream is
= p3
.p
.getInputStream();
51 InputStreamReader isr
= new InputStreamReader(is
);
52 BufferedReader br
= new BufferedReader(isr
);
53 String result
= br
.readLine();
54 if (! "Goodbye World".equals(result
))
56 System
.out
.println("bad 1");
59 result
= br
.readLine();
62 System
.out
.println("bad 2");
65 int c
= p3
.p
.waitFor();
66 System
.out
.println(c
== 0 ?
"ok" : "bad 3");
70 System
.out
.println(ex
.toString());