Storing Shell Output
Posted
by Emil Radoncik
on Stack Overflow
See other posts from Stack Overflow
or by Emil Radoncik
Published on 2010-04-22T12:37:02Z
Indexed on
2010/04/29
7:37 UTC
Read the original article
Hit count: 370
Hello everybody,
I am trying to read the output of a shell command into a string buffer, the reading and adding the values is ok except for the fact that the added values are every second line in the shell output. for example, I have 10 rows od shell output and this code only stores the 1, 3, 5, 7, 9, row . Can anyone point out why i am not able to catch every row with this code ??? any suggestion or idea is welcomed :)
import java.io.*;
public class Linux {
public static void main(String args[]) {
try {
StringBuffer s = new StringBuffer();
Process p = Runtime.getRuntime().exec("cat /proc/cpuinfo");
BufferedReader input =
new BufferedReader(new InputStreamReader(p.getInputStream()));
while (input.readLine() != null) {
//System.out.println(line);
s.append(input.readLine() + "\n");
}
System.out.println(s.toString());
} catch (Exception err) { err.printStackTrace(); } } }
© Stack Overflow or respective owner