0

This is my code

private final static String Dir = "/data/data/org.xbmc.kodi/cache/";
private void deleteDir(){
    try{
        /*File dir = Utils.getCacheDir(null, this, false, true);
        File path = new File(dir.getParentFile(), Dir);
        delete(path);*/
        String command = "rm -r "+Dir;
        Process process = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(process.getOutputStream());
        os.writeBytes(command + "\n");
        os.writeBytes("exit\n");
        os.flush();
        process.waitFor();
    }catch(Exception e){}
}

The issue is when I run this it deletes the folder. But I don't want to delete the folder. I want to delete all the files inside it.

mkj
  • 2,761
  • 5
  • 24
  • 28

2 Answers2

6
File dir = new File(Environment.getExternalStorageDirectory()+"Dir_name_here"); 
if (dir.isDirectory()) 
{
    String[] children = dir.list();
    for (int i = 0; i < children.length; i++)
    {
       new File(dir, children[i]).delete();
    }
}

source thread.

Community
  • 1
  • 1
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41
4

Use this:

File file = new File("your dir");        
String[] files;      
files = file.list();  
for (int i=0; i<files.length; i++) {  
    File myFile = new File(file, files[i]);   
    myFile.delete();  
} 
Michele Lacorte
  • 5,323
  • 7
  • 32
  • 54