I have the following code.
public void eliminarCarpeta (){
JOptionPane.showMessageDialog(null, "La ruta de la carpeta a eliminar es: "+rutaFotos);
File carpetaCreada = new File(rutaFotos);
if (carpetaCreada.delete()==true) {
System.out.println("Carpeta Eliminada");
} else {
System.out.println("No se pudo eliminar la carpeta");
}
}
And my problem may be simple but I don't know how to do it. It turns out that I use this method to delete folders from "x" directory and the inconvenience that I am presenting is that when the folder has content, the method enters the else telling me that it could not delete it.
When I delete the contents of the folder (manually from windows) and run the method again, it succeeds in deleting the folder. I don't know if I'm using a method that only serves to delete empty folders or if I'm running it wrong.
The only thing I need is for the method to remove the folder from the path that it sends whether it has content or not.
Thank you very much
You should resort to recursion. For this, what you are looking for would be:
Where you would go looping through the elements within the directories and deleting their content before deleting the element itself.