-4

I tried to use this code to delete a file located into /data folder but it doesn't work, what's wrong in it? My device has root.

Runtime.getRuntime().exec(new String[]{"su","rm"+" "+"/data/logger"});

SOLVED USING THIS

Process p;
            try {
                    p = Runtime.getRuntime().exec("su");
                    DataOutputStream os = new DataOutputStream(p.getOutputStream());
                 os.writeBytes("rm /data/logger"+"\n");
            os.writeBytes("exit\n");
            os.flush();
            p.waitFor();
            } catch (IOException e) {
                    e.printStackTrace();
            } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                    }
Hasta'98
  • 184
  • 2
  • 9
  • Generally, for `rm` to delete a non-empty directory you need the -r flag in possible addition to the -f one. That would be apart from the issue of having a working "su" hack and passing arguments in a working way. – Chris Stratton May 02 '14 at 17:24
  • You should probably use a [ProcessBuilder](http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ProcessBuilder.html) because that will actually format your command correctly. And per the [javadoc](http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exec%28java.lang.String%5B%5D,%20java.lang.String%5B%5D,%20java.io.File%29) - "ProcessBuilder.start() is now the preferred way to start a process with a modified environment." – Elliott Frisch May 02 '14 at 17:31
  • Please give me an example with code. – Hasta'98 May 02 '14 at 17:32

1 Answers1

0

Use the -f switch to force the delete.

File exists case is taken care by runtime class.

Try this:

Runtime.getRuntime().exec(new String[]{"su","rm","-f","/data/logger"});

or this

Runtime.getRuntime().exec("su")

Runtime.getRuntime().exec(new String[]{"rm","-f","/data/logger"});

Sireesh Yarlagadda
  • 12,978
  • 3
  • 74
  • 76