Sep
23
|
Use the listFiles() method of the File class to iterate through all the files in the directory. The File classes delete() method is then used to delete each file.
Remember to check the return value from the call to verify it was deleted successfully.
File directory = new File(directoryName); // Get all files in directory File[] files = directory.listFiles(); for (File file : files) { // Delete each file if (!file.delete()) { // Failed to delete file System.out.println("Failed to delete "+file); } }
Array ( ) 2 Responses to “How to delete all files in a directory?”
Leave a Reply
You must be logged in to post a comment.
February 19th, 2010 at 11:35 am
Just what I needed in our Exam in java…HAHA!!!
May 12th, 2013 at 8:06 am
Great ^_^