简体   繁体   中英

Open files using root android

In java, after getting root permissions, how can I open files with it? Can I open them with File class or do I have to use a command?

You will have to use the su command.

I suggest you have a look at how to use su

Sample code to read a file ( I haven't tested it but it should give you an idea ):

public static void runAsRoot(String[] cmds){
    try {
       Process p = Runtime.getRuntime().exec("su");
       BufferedReader reader = new BufferedReader(p.getInputStream());
       StringBuilder out = new StringBuilder();

       String line;
       while ((line = reader.readLine()) != null) {
           out.append(line);   // add everything to StringBuilder 
           // here you can have your logic of comparison.
           if(line.toString().equals(".")) {
               // do something
           } 
       }

   } catch(IOException e) {
       e.printStackTrace();
   } catch(InterruptedException e) {
       e.printStackTrace();
 }

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