How to Delete a Folder with Files Using Java

How to delete a folder with files using Java

Java isn't able to delete folders with data in it. You have to delete all files before deleting the folder.

Use something like:

String[]entries = index.list();
for(String s: entries){
File currentFile = new File(index.getPath(),s);
currentFile.delete();
}

Then you should be able to delete the folder by using index.delete()
Untested!

deleting folder from java

If you use Apache Commons IO it's a one-liner:

FileUtils.deleteDirectory(dir);

See FileUtils.deleteDirectory()



Guava used to support similar functionality:

Files.deleteRecursively(dir);


This has been removed from Guava several releases ago.


While the above version is very simple, it is also pretty dangerous, as it makes a lot of assumptions without telling you. So while it may be safe in most cases, I prefer the "official way" to do it (since Java 7):

public static void deleteFileOrFolder(final Path path) throws IOException {
Files.walkFileTree(path, new SimpleFileVisitor<Path>(){
@Override public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs)
throws IOException {
Files.delete(file);
return CONTINUE;
}

@Override public FileVisitResult visitFileFailed(final Path file, final IOException e) {
return handleException(e);
}

private FileVisitResult handleException(final IOException e) {
e.printStackTrace(); // replace with more robust error handling
return TERMINATE;
}

@Override public FileVisitResult postVisitDirectory(final Path dir, final IOException e)
throws IOException {
if(e!=null)return handleException(e);
Files.delete(dir);
return CONTINUE;
}
});
};

Delete all files in directory (but not directory) - one liner solution

import org.apache.commons.io.FileUtils;

FileUtils.cleanDirectory(directory);

There is this method available in the same file. This will also recursively deletes all sub-folders and files under them.

Docs: org.apache.commons.io.FileUtils.cleanDirectory

How to delete a folder containing other folders in Java?

To delete folder having files , no need of loops or recursive search. You can directly use:

FileUtils.deleteDirectory(<File object of directory>);

This function will delete the folder and all files in it

How to delete files AND folders with specific names, and all of the files within those folders with Java

You can use Apache Commons IO FileUtil (or at least have a look at the source) instead of File.delete():

FileUtil.deleteDirectory(newDirectoryStreamItem);

You can’t use delete() (naively) since you recursively need to delete the content of a subdir before deleting the directory itself.

Delete directories recursively in Java

You should check out Apache's commons-io. It has a FileUtils class that will do what you want.

FileUtils.deleteDirectory(new File("directory"));

Create directory. If exists, delete directory and its content and create new one in Java

public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
return dir.delete();
}


Related Topics



Leave a reply



Submit