I am trying to delete all files from my directory(without deleting the directory itself) and I looked around on stackoverflow, none of the solutions I found did work neither did my own code. It seemed to delete some files but for others it didn't and I don't understand why. The files dewi1, dewi2, dewi3 etc. are all copies of each other but renamed, when running this code none of these files were open or in use.
File directory = new File("C:/Users/TUDelftSID/Downloads/Primal-PvM/Primal-PvM/pox/data/logs/Packetlogs");
File[] allFilesInDir = directory.listFiles();
for (int K = 0; K < allFilesInDir.length; K++) {
if (allFilesInDir[K].delete()) {
System.out.println("Succesfully Deleted: " + allFilesInDir[K] + " !");
} else {
System.out.println("Failed to delete " + allFilesInDir[K] + " !");
}
}
This is What I used, as far as I can tell, the files should all be deleted; however this is the result.
Why is this going on?
I already tried this aswell:
for(File file: directory.listFiles())
if (!file.isDirectory())
file.delete();
And this one:
Arrays.stream(directory).listFiles()).forEach(File::delete);
Both did not work :/, how to solve this problem?