对于“su”子进程,我应该将命令写入输出还是输入流?
为了获得android的root权限,我们通常这样做:
Process p=Runtime.getRuntime().exec(“su”);
DataOutputStream stream=new DataOutputStream(p.getOutputStream());
stream.writeBytes("mkdir /testFolder\n");
stream.writeBytes(“exit \n”);
p.waitFor();
执行上面的代码,我们可以创建一个文件夹/testFolder,一切都很好,但我对此感到困惑。你知道,当我们想在终端中执行某些命令时,我们首先输入一些代码,然后程序读取输入缓冲区以对代码执行某些操作。但是这里我们将字符串写入到子进程的输出流中,为什么呢?似乎 te sub-progress 从其输出缓冲区读取命令,而不是输入缓冲区?
To gain the root permission of android, we usually do like this:
Process p=Runtime.getRuntime().exec(“su”);
DataOutputStream stream=new DataOutputStream(p.getOutputStream());
stream.writeBytes("mkdir /testFolder\n");
stream.writeBytes(“exit \n”);
p.waitFor();
Execute the codes above, we can create a folder /testFolder, everything is OK, but i feel confused about it. you know, when we want to execute some command in the terminal, we first input some codes, and the program read the input buffer to do something with the codes. But here we write string to the output stream of the sub-process, why? it seems that te sub-progress read command from its output buffer, not input buffer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许命名有点奇怪,但是 Process.getOutputStream( ) 返回一个连接到进程标准输入的
OutputStream
。这些名称是从父进程的角度来看的。父进程的输出是子进程的输入。父进程的输入是子进程的输出。
Maybe the naming is a little weird, but Process.getOutputStream() returns an
OutputStream
connected to the standard input of the process.The names are from the point of view of the parent process. The parent process's output is the subprocess's input. The parent process's input is the subprocess's output.