Saving shell output

I am trying to read the output of a shell command into a string buffer, reading and adding values ​​in order, except for the fact that the added values ​​represent every second line in the shell output. for example, I have 10 lines od shell output, and this code only stores 1, 3, 5, 7, 9, line. Can anyone point out why I can't catch every line with this code ??? any suggestion or idea is welcome :)

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();
}    }
}
+5
source share
2 answers

Here is the code that I usually use with BufferedReader in such situations:

StringBuilder s = new StringBuilder();
Process p = Runtime.getRuntime().exec("cat /proc/cpuinfo");
BufferedReader input =
    new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
//Here we first read the next line into the variable
//line and then check for the EOF condition, which
//is the return value of null
while((line = input.readLine()) != null){
    s.append(line);
    s.append('\n');
}

, , StringBuilder StringBuffer StringBuffer.

+7

, input.readLine(), . , while(), . .

+3

All Articles