Fahim Parkar Fahim Parkar - 1 year ago 121
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 Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download