
Originally Posted by
ibosstech
Hi all
I am new to this forum so would like to greet all who have been here since long helping others and wish i will get the same response too. I have a lot of questions to ask but am beginning with a problem that i am currently stuck in.
How to do piping in java? i want to copy the input given by a user in a particular software to the clipboard and get it pasted to any other software. For this i wanna use piping concept in java
Can anyone help to pipe two objects in java?
Thanks
Unlike scripting language, Java is a compiled language and therefore piping feature you want in Java is via the Java SDK API. Please see below sample.
Code:
try {
String cmdString = "<put the command you want to read it's output from here>";
Runtime r = Runtime.getRuntime();
Process child = r.exec(cmdString);
InputStreamReader isr = new InputStreamReader(child.getInputStream());
BufferedReader br = new BufferedReader(isr);
String line = null;
while ( (line = br.readLine()) != null) System.out.println(line);
int res = child.waitFor();
} catch (InterruptedException ie) {
System.out.println(ie.getMessage());
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
Bookmarks