Read realtime output of a command
<p>How can I read real-time output of a shell command from Java</p>

<p>This is what I have so far but it prints after command has been executed:</p>

<pre><code>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) {
        publishProgress(i);
        i++;
        System.out.println(line); // it prints all at once after command has been executed.
    }
}
catch (IOException e) {
    Log.e("du","error "+e.getMessage());
}
</code></pre>
