【问题标题】:Android Runtime.getRuntime().exec() to nav through directoriesAndroid Runtime.getRuntime().exec() 导航目录
【发布时间】:2011-06-04 20:30:26
【问题描述】:

所以我希望能够编写一个可以打开和显示 logcat 消息、dmesg 的应用程序,并且还能够运行诸如 'ls' 'cat' 'echo' 'cd' 之类的命令。

如果我执行以下操作:

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;
        }

我可以将文本“完整”放入文本视图并查看根目录。

不过,我能做的也就这么多了。假设我想找到一个目录,然后切换到它,我遇到了麻烦。

如果我这样做:

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;
        }

“完整”和“文本”都没有得到任何结果

有什么想法吗?

【问题讨论】:

    标签: android command-line terminal runtime.exec


    【解决方案1】:

    当前工作目录与当前进程相关联。当您执行(“cd tmp”)时,您正在创建一个进程,将其目录更改为“tmp”,然后该进程退出。父进程的工作目录不会改变。

    有关changing the current working directory in Java 的更一般性讨论,请参阅此内容。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-01-09
          • 2015-08-28
          • 2012-12-07
          • 1970-01-01
          • 2012-06-21
          • 2020-08-05
          相关资源
          最近更新 更多