Reputation: 1095
I am writing some code that deletes all the files in a directory, so far I have:
@ViewScoped
@ManagedBean
public class Delete {
public void DeleteFiles() throws IOException {
System.out.println("Called deleteFiles");
File file = new File("D:/Documents/NetBeansProjects/printing~subversion/fileupload/build/web/resources/pdf/up617648/");
String[] myFiles;
if (file.isDirectory()) {
myFiles = file.list();
for (int i = 0; i < myFiles.length; i++) {
File myFile = new File(file, myFiles[i]);
System.out.println(myFile);
myFile.delete();
}
}
}
}
When I call this I can see all the files are called etc but nothing ever gets deleted, why is this, this is what I get in the console:
INFO: Called deleteFiles
INFO: D:\Documents\NetBeansProjects\printing~subversion\fileupload\build\web\resources\pdf\up617648\1.png
INFO: D:\Documents\NetBeansProjects\printing~subversion\fileupload\build\web\resources\pdf\up617648\lecture04_A_slides.pdf
All the files in the path are deleted, yet in Netbeans I can still see the files in the resources folder, even though the direct path to this folder is empty, I have done a clean build.
This is a bit weird, it deletes the files from the directory fine, in Netbeans it still shows up the files, restart netbeans and then the files appear back into the directory.
Upvotes: 1
Views: 489
Reputation: 29367
Windows file system isn't really friendly to the way Java does deletions. Most likely something else holds a reference/handle to that file which makes Windows prevent its deletion. This something may be quite literally anything, even the JVM itself if you've touched the same file previously and haven't cleaned up resources properly.
Upvotes: 1