简体   繁体   中英

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.'

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.

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. The parent process' working directory doesn't change.

See this for a more general discussion of changing the current working directory in Java .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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