Saturday, June 11, 2016

Deleting files in a folder using Java

To delete the files in a folder using java, the following piece of code can be used.

String path = "Path\\to\\target\\folder\\";

File folder = new File(path);
File[] listOfFiles = folder.listFiles();

//Iterate through the files
for (File f : listOfFiles) {
    f.delete();
}

The folder itself can also be deleted by adding the following line after the for loop.

folder.delete();

No comments:

Post a Comment