gpt4 book ai didi

java - 读取命令的实时输出

转载 作者:太空宇宙 更新时间:2023-11-03 13:30:48 25 4
gpt4 key购买 nike

如何从 Java 读取 shell 命令的实时输出

这是我目前所拥有的,但它在执行命令后打印:

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());
}

最佳答案

试试这个:

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());
}

关于java - 读取命令的实时输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13530429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com