在Android中执行进程以读取文件

在Android中执行进程以读取文件,android,linux,command,Android,Linux,Command,我需要使用在Android中使用Java执行的Linux Shell命令读取文件内容。我需要执行什么命令才能读取文件中的所有文本并保存到字符串对象中 注意,我不能使用简单的Java I/O函数!我需要读取的文件位于设备的系统目录中 String command= ""; String file_path = "misc/file.txt"; StringBuffer output = new StringBuffer(); Process p; try

我需要使用在Android中使用Java执行的Linux Shell命令读取文件内容。我需要执行什么命令才能读取文件中的所有文本并保存到字符串对象中

注意,我不能使用简单的Java I/O函数!我需要读取的文件位于设备的系统目录中

   String command= "";
   String file_path = "misc/file.txt";
   StringBuffer output = new StringBuffer();

      Process p;
      try {
        p = Runtime.getRuntime().exec(command);
        p.waitFor();
        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

        String line = "";     
        while ((line = reader.readLine())!= null) {
          output.append(line + "\n");
        }

      } catch (Exception e) {
        e.printStackTrace();
      }
      String response = output.toString();
够了。你为什么不遵循简单的解决方案呢

更新

/**
 * Execute a command in a shell
 * 
 * @param command
 *            command to execute
 * @return the return of the command
 */
public String exec(String command) {
    String retour = "";
    try {
        Runtime runtime = Runtime.getRuntime();

        Process p = runtime.exec(command);

        java.io.BufferedReader standardIn = new java.io.BufferedReader(
                new java.io.InputStreamReader(p.getInputStream()));
        java.io.BufferedReader errorIn = new java.io.BufferedReader(
                new java.io.InputStreamReader(p.getErrorStream()));
        String line = "";
        while ((line = standardIn.readLine()) != null) {
            retour += line + "\n";
        }
        while ((line = errorIn.readLine()) != null) {
            retour += line + "\n";
        }
    } catch (java.io.IOException e) {
        e.printStackTrace();
    }

    return retour;
}

将其作为exec调用(“cat misc/file.txt”)

“我需要使用在Android中使用Java执行的Linux Shell命令读取文件的内容”——嗯,为什么不使用Java文件I/O读取文件?因为文件在系统中,我无法使用简单的文件I访问它/O@ZiadHalabi您的问题解决了吗?@Chakkappan我将在几个小时后测试它。因为我无法使用简单的I/O功能访问该文件。该文件位于设备的系统中。
/**
 * Execute a command in a shell
 * 
 * @param command
 *            command to execute
 * @return the return of the command
 */
public String exec(String command) {
    String retour = "";
    try {
        Runtime runtime = Runtime.getRuntime();

        Process p = runtime.exec(command);

        java.io.BufferedReader standardIn = new java.io.BufferedReader(
                new java.io.InputStreamReader(p.getInputStream()));
        java.io.BufferedReader errorIn = new java.io.BufferedReader(
                new java.io.InputStreamReader(p.getErrorStream()));
        String line = "";
        while ((line = standardIn.readLine()) != null) {
            retour += line + "\n";
        }
        while ((line = errorIn.readLine()) != null) {
            retour += line + "\n";
        }
    } catch (java.io.IOException e) {
        e.printStackTrace();
    }

    return retour;
}