简体   繁体   English

Android Runtime.getRuntime()。exec()导航目录

[英]Android Runtime.getRuntime().exec() to nav through directories

So I want to be able to write an app that can turn on and display logcat messages, dmesg, and also be able to run commands like 'ls' 'cat' 'echo' 'cd.' 所以我希望能够编写一个可以打开并显示logcat消息,dmesg的应用程序,并且还能够运行像'ls''cat''echo''cd'这样的命令。

If I do the following: 如果我执行以下操作:

nativeProc = Runtime.getRuntime().exec("ls\n");
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(nativeProc.getOutputStream()));
        BufferedReader in = new BufferedReader(new InputStreamReader(nativeProc.getInputStream()));
        String line = null;

        while ((line = in.readLine()) != null) {  
            full = full + "\n" + line;
        }

I can put the text "full" to a Text View and see the root directory. 我可以将文本“full”放到Text View中并查看根目录。

However, that's about all I can do. 但是,这就是我所能做的一切。 Let's say I want to find a directory, and change to it, I'm having trouble. 假设我想要找到一个目录,然后更改它,我遇到了麻烦。

So if I do this: 所以,如果我这样做:

nativeProc = Runtime.getRuntime().exec("ls\n");
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(nativeProc.getOutputStream()));
        BufferedReader in = new BufferedReader(new InputStreamReader(nativeProc.getInputStream()));
        String line = null;

        while ((line = in.readLine()) != null) {  
            full = full + "\n" + line;
        }
        /* Code here to confirm the existing of a directory*/


        nativeProc = Runtime.getRuntime().exec("cd tmp\n");
        BufferedReader in2 = new BufferedReader(new InputStreamReader(nativeProc.getInputStream()));
        line = null;
        String test = "\nStart1:\n";
        while ((line = in2.readLine()) != null) {  
            test = test + "\n" + line;
        }

I get nothing for both "full" and "text" “完整”和“文字”都没有任何结果

Any ideas? 有任何想法吗?

您可以使用提供的路径执行ls,以列出应列出的文件夹

Runtime.getRuntime().exec(new String[] { "ls", "\\tmp"});

The current working directory is associated with the current process. 当前工作目录与当前进程相关联。 When you exec("cd tmp") you are creating a process, changing its directory to "tmp", and then the process exits. 当您执行exec(“cd tmp”)时,您正在创建一个进程,将其目录更改为“tmp”,然后进程退出。 The parent process' working directory doesn't change. 父进程的工作目录不会更改。

See this for a more general discussion of changing the current working directory in Java . 有关更改Java中当前工作目录的更一般性讨论,请参阅此处

如何编写一个shell文件来检查该文件是否存在以及该文件中的所有其他实现并将其添加到sdcard并使用getRuntimr.exec()从java触发shell文件。您还可以将参数传递给它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM