Fahim Parkar Fahim Parkar - 4 months ago 36
Java Question

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

I want to delete all files inside ABC directory.

When I tried with

FileUtils.deleteDirectory(new File("C:/test/ABC/"));
it also deletes folder ABC.

Is there a one liner solution where I can delete files inside directory but not directory?

Answer
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

Comments