Reputation: 1243
Need advise on how to implement pipes in Java. Eg,
echo "test"|wc
I need to show results of the above pipe example.
I have tried this:
public class myRunner {
private static final String[] cmd = new String[] {"wc"};
public static void main(String[] args){
try {
ProcessBuilder pb = new ProcessBuilder( cmd );
pb.redirectErrorStream(true);
Process process = pb.start();
OutputStream os = process.getOutputStream();
os.write("echo test".getBytes() );
os.close();
}catch (IOException e){
e.printStackTrace();
}
How can I view the output of wc
output?
I believe another one of the library i can use is PipedInputStream
/PipedOutputStream
. Can anyone show an example on how to use it? Am quite confused. thanks
Upvotes: 0
Views: 1844
Reputation: 11
public ArrayList<String> executorPiped(String[] cmd, String outputOld){
String s=null;
ArrayList<String> out=new ArrayList<String>();
try {
ProcessBuilder pb = new ProcessBuilder( cmd );
pb.redirectErrorStream(true);
Process p = pb.start();
OutputStream os = p.getOutputStream();
os.write(outputOld.getBytes());
os.close();
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
while ((s = stdInput.readLine()) != null) {
out.add(s);
}
}catch (IOException io){
}
return out;
}
Upvotes: 0
Reputation: 310980
How can I view the output of wc output?
By consuming the Process's
output, via Process.getInputStream()
, and reading from it.
Upvotes: 1