0

如何读取存储在根目录的 Android 手机内部目录中的文件的内容?

我正在使用以下内容,但它不起作用,我变得空白:

    filePath = "/data/";
    File file = new File(filePath, "test.conf");
    StringBuilder text = new StringBuilder();
try {
    BufferedReader br = new BufferedReader(new FileReader(file));
    while ((line = br.readLine()) != null) {
        text.append(line);
        text.append('\n');
    }
}
catch (IOException e) {

}
return text.toString(); 

知道有什么问题吗?

4

1 回答 1

0

使用@Blade0rz 给出的链接中的示例,我对其进行了修改以从内部存储器中获取文件。基本上,我将内容复制到 sdcard 上的测试文件中。随后,我可以用它做任何事情,之后不需要 root 访问权限。

           Process p;
           try {
               // Preform su to get root privledges
               p = Runtime.getRuntime().exec("su");

               // Attempt to write a file to a root-only
               DataOutputStream os = new DataOutputStream(p.getOutputStream());

               String path =  Environment.getExternalStorageDirectory().getPath() + "/test.txt";
               os.writeBytes("cat /data/test.conf >" + path +"\n");
               // Close the terminal
               os.writeBytes("exit\n");
               os.flush();

              Toast.makeText(getApplicationContext(), path, Toast.LENGTH_LONG).show();

               try {
                   p.waitFor();
                   if (p.exitValue() != 255) {
                       // TODO Code to run on success
                       Toast.makeText(getApplicationContext(), "Phone is Rooted.", Toast.LENGTH_LONG).show();
                   }
                   else {
                       // TODO Code to run on unsuccessful
                       Toast.makeText(getApplicationContext(), "Phone is Not Rooted.", Toast.LENGTH_LONG).show();
                   }
               } catch (InterruptedException e) {
                   // TODO Code to run in interrupted exception
                   Toast.makeText(getApplicationContext(), "Phone is Not Rooted.", Toast.LENGTH_LONG).show();
               }
           } catch (IOException e) {
               // TODO Code to run in input/output exception
               Toast.makeText(getApplicationContext(), "Phone is Not Rooted.", Toast.LENGTH_LONG).show();
           }
于 2013-07-31T06:38:45.007 回答