How to Delete Files in Directory Using Java
Let’s say you have a folder with files that you want to delete. The following code illustrates how to do this. You could also combine it with the code in this post to recursively delete files in subdirectories as well.
import java.io.*; ... public static void DeleteFilesInDirectory(String dir) { String[] fileNames = new File(dir).list(); for (String fileName : fileNames) { File file = new File(dir + fileName); if (file.exists()) file.delete(); } }