我试图使用执行命令行,CD不起作用
try {
Process process = Runtime.getRuntime().exec(command);
BufferedReader stream = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = stream.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append('\n');
}
stream = new BufferedReader(new InputStreamReader(process.getErrorStream()));
while ((line = stream.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append('\n');
}
} catch (IOException e) {
e.printStackTrace();
}
ls
工程执行cd
。但是cd dir
不起作用。它不会更改目录。如果我执行pwd
,它仍显示/
。我读到每次执行Runtime.getRuntime().exec(command);
时,它都会创建一个新的shell。我想我需要捕获这个shell并且维护它,直到用户关闭应用程序。
不仅如此,我没有在我的输出中得到任何错误。例如。当我放置一个不存在的命令时,我没有收到错误信息。
你还没有描述什么“不工作”在这里的意思。它是否会抛出异常?由于'cd'命令不会输出任何成功(不像'ls'),你怎么知道它失败了?另外 - 你试图更改什么目录? Android对允许访问哪些目录有很多限制。 – adelphus
我已经更新了答案。如果我尝试“cd”到不存在的目录,它也不会给我任何错误。任何想法,为什么我没有收到错误信息? –