Deleting files from a folder - android

I have a folder which contains some files that I want to deleted after processing them. The files have the extension .FIR After some googling I found a recursive method that I modified a bit:

void delete(File f) throws IOException {
      if (f.isDirectory()) {
        for (File c : f.listFiles())
            if(f.listFiles().toString().contains(".FIR"))
                delete(c);
      }
      if (!f.delete())
        throw new FileNotFoundException("Failed to delete file: " + f);
    }

This function will throw an IOException telling me:

07-31 11:02:31.885: E/DELETE:(5694): Failed to delete file: /mnt/sdcard/ExtractedFiles

The folder has been set to RW operations. And in my manifest file:

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

I couldn't find another permission that sounded something like MODIFY_FILES

¿Alguna idea?

preguntado el 31 de julio de 12 a las 10:07

el porqué if(f.listFiles).toString().contains.... You have to check c extension right? Not if your current folder has some .FIR file. -

5 Respuestas

prueba esto:

void delete(File f) throws IOException {
    if (f.isDirectory()) {
        for (File c : f.listFiles()) {
            delete(c);
        }
    } else if (f.getAbsolutePath().endsWith("FIR")) {
        if (!f.delete()) {
            new FileNotFoundException("Failed to delete file: " + f);
        }
    }
}

respondido 13 mar '17, 06:03

what is the logic behind FIR? - narendra singh

Mejor uso temp archivos ....

File f = File.createTempFile("pattern", ".suffix");

Once the application is closed, the temp files are first closed then deleted.

Consulte este enlace para obtener más detalles:

http://www.roseindia.net/java/example/java/io/create-temp-file.shtml

Respondido 31 Jul 12, 10:07

Creo que el problema está aquí:

if(f.listFiles().toString().contains(".FIR"))

cambie esto a:

if(c.getName().contains(".FIR"))

And make sure your directory contains files with extension .FIR only, otherwise (if any other extension files are available) it will still fail to delete a non-empty directory


Otherwise use the following method to get it done:

private static boolean delete(File dir) {
    if (dir != null && dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            boolean success = delete(new File(dir, children[i]));
            if (!success) {
                return false;
            }
        }
    }
    return (dir.getName().contains(".FIR"))? dir.delete() : false;
}

Respondido 31 Jul 12, 10:07

The exception will no* be thrown if I do so, but the files in the directory will not be deleted. - Tobias Moe Thorstensen

comprobar esta

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

Respondido 31 Jul 12, 10:07

The same exception is thrown. - Tobias Moe Thorstensen

i was using this code and it;s working:

 String root_sd = Environment.getExternalStorageDirectory().toString();
 File file = new File(path) ;       
  File list[] = file.listFiles();
    for(File f:list)
      {
     name =  file.getName();
    filestv.setText(f.getName());
    //add new files name in the list
   //  delete.setText(name );


      }  


}

and you can follow the complete tutoriales

Respondido el 17 de junio de 15 a las 02:06

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.