Reputation: 85
I want to delete a Folder with its content. If the file-structure is the following:
TestFolder
-Folder1
--File1
-Folder2
--File2
--File3
The Array should contain the Files in the order: {Folder1,Folder2,File1,Folder3,File2,File3}, by using the code:
import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.nio.file.Files;
class Folder{
private int filesCount = 0;
private File[] files;
private int counter = 0;
private void getFilesArray() throws IOException{
paths = new String[countFiles(path)];
thisFolder(path);
}
private void countFiles(File folder) throws IOException{
File[] content = (new File(folder)).listFiles();
for (File file:content){
if (file.isDirectory()){
countFiles(file);
}
filesCount++;
}
}
private void thisFolder(File folder) throws IOException{
if (folder.exists()){
File[] content = file.listFiles();
for (File file:content){
files[counter] = file;
counter++;
if (file.isDirectory()){
thisFolder(file);
}
}
}else{
throw new IOException("Folder was deleted while it was still opened: "+file.getAbsolutePath());
}
}
//...
}
I want to delete the content of the Folder by using the File[]-Array:
public boolean delete(){
for (int count = filesCount-1; count >= 0;count--){
if(files[count].exists()){
if(files[count].delete()==false){
System.out.println("Failed to delete "+files[count]);
for (String temp:files[count].list())
System.out.println(" remaining content "+temp);
return false;
}else{
System.out.println("Properly deleted "+files[count].getAbsolutePathPath());
}
}
}
if (folder.delete()){
return true;
}else{
System.out.println(2);
return false;
}
}
I need the File[]-Array in multiple further methods and actually want the Folder-object I´m creating to contain the -perhaps old- content of the moment I´m creating it. The delete method shall return false if not worked. Errorhandling is in a different class. Now the issue:
It deletes all Files propery by the first run and every Folder which was empty before I ran the program, but it Fails to delete the folders which had content before I ran the program, even if they´re empty now.
Here the debug output (first run):
//Properly deleted Folder2\File3.txt
//Properly deleted Folder2\File2.txt
//Failed to delete Folder2
// remaining content File2.txt
// remaining content File3.txt
//program aborted
Here the debug output (second run):
//Properly deleted Folder2
//Properly deleted Folder1\File1.txt
//Failed to delete Folder1
// remaining content File1.txt
//program aborted
Here the debug output (third run):
//Properly deleted Folder1
//Properly deleted TestFolder
//program completed
The directory-of course- is only for the program to test, so I´m sure there are no other (hidden) files in it. There is -for the testing-also no different program or method using the folders content, so even if it can be different when I´ll use the code for what I programmt it, for this test I´m absolutly sure that the File[]-Array is actual! I´ve even testsed the recursively method from comment below, but still get the same problem, even if the debug output is a bit different, becouse the order I visit Files and Folders is different. Is there any trick to make the file actual again? Maybe a command like File.update();
Thanks for any help :)
Upvotes: 0
Views: 326
Reputation: 85
I finally found out, why the delete() method didn´t worked out! In the method to count the size of the folder (so 0mB for this) I opened a fileChannel and forgot to close it properly. It seems as that coused the issue. Now that I added the close() command it works just fine... What a stupid mistake ^^ Thanks to anyone who tryed on helping me :)
Upvotes: 0
Reputation: 9058
You're off track. First, drop the sleep. It's not necessary.
Next, how sure are you that you're deleting in the proper order? I personally wouldn't delete it the way you're doing it. I'd make a recursive method that deletes the file and it's contents.
void deleteMe(File file) {
if (file.isDirectory()) {
for (File subfile: file.listFiles) {
deleteMe(subfile);
}
}
file.delete();
}
For debugging your code above, before the call to delete, I might add a debug like:
System.out.printf("File: %s. Number of subfiles: %d\n", file.getName(), file.listFiles().length).
(Something like that.)
Just to make sure it's doing what you think it's doing.
Upvotes: 3