Skip to content Skip to sidebar Skip to footer

Read Realtime Output Of A Command

How can I read real-time output of a shell command from Java This is what I have so far but it prints after command has been executed: try { proc = Runtime.getRuntime().exec('d

Solution 1:

Try this:

try {
 proc = Runtime.getRuntime().exec("du -d 1 /sdcard/");

 InputStream inputStream = proc.getInputStream();
 InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
 BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

 while ((line = bufferedReader.readLine()) != null) {
    System.out.println(line); // it prints all at once after command has been executed.
 }
 proc.waitFor();
}
catch (IOException e) {
 Log.e("du","error "+e.getMessage());
}

Post a Comment for "Read Realtime Output Of A Command"