Android java程式碼中如何執行shell命令
阿新 • • 發佈:2019-02-11
public void execCommand(String command) throws IOException {
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(command);
try {
if (proc.waitFor() != 0) {
System.err.println("exit value = " + proc.exitValue());
}
BufferedReader in = new BufferedReader(new InputStreamReader(
proc.getInputStream()));
StringBuffer stringBuffer = new StringBuffer();
String line = null;
while ((line = in.readLine()) != null) {
stringBuffer.append(line+"-");
}
System.out.println(stringBuffer.toString());
} catch (InterruptedException e) {
System.err.println(e);
}
}
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(command);
try {
if (proc.waitFor() != 0) {
System.err.println("exit value = " + proc.exitValue());
}
BufferedReader in = new BufferedReader(new InputStreamReader(
proc.getInputStream()));
StringBuffer stringBuffer = new StringBuffer();
String line = null;
while ((line = in.readLine()) != null) {
stringBuffer.append(line+"-");
}
System.out.println(stringBuffer.toString());
} catch (InterruptedException e) {
System.err.println(e);
}
}