Results 1 to 5 of 5
Thread: deleting a full directory
- 10-10-2009, 02:02 AM #1
Member
- Join Date
- Sep 2008
- Posts
- 16
- Rep Power
- 0
- 10-10-2009, 02:06 AM #2
Member
- Join Date
- Sep 2008
- Posts
- 16
- Rep Power
- 0
this is my code to delete the folder and Everything inside it
Java Code:public static void deletedir(File dir){ File[] list = dir.listFiles(); for(int x = 0 ; x < list.length; x++){ if(list[x].isFile()){ list[x].delete(); }else if(list[x].isDirectory()){ deletedir(list[x]); } } dir.delete(); }
- 10-10-2009, 02:07 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Delete each of the elements of the directory. Do this recursively if any of the contents of the directory are, themselves, directories.
Possibly better: check that you have permissions to delete the contents before you begin deleting them (again this will, in general, be recursive). That way if there are permissions problems you won't be left with data in a random half-deleted state.
---
Just saw your second post. That looks OK.
- 10-10-2009, 02:13 AM #4
Member
- Join Date
- Sep 2008
- Posts
- 16
- Rep Power
- 0
how do you check if they have permission?
do you just check if the file has been successfully deleted ?
- 10-10-2009, 02:32 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
That's a good question!
If you think there might be a danger that deep within the directories is a file that you can't delete for some reason and if you really don't want to leave the directory in a half deleted state then it would be no good checking after you had deleted things.
As far as I can see there is no method in File for checking whether you can actually delete things. Or even for checking whether a file is writable. About the only thing I can think of in such a case is to make a temporary copy and using it to restore the original in case that checking reveals that the delete failed.
Similar Threads
-
how to get the full file name
By priyanka3006 in forum JDBCReplies: 0Last Post: 08-05-2009, 12:55 PM -
Full screen test
By Java Tip in forum java.awtReplies: 0Last Post: 06-23-2008, 11:24 PM -
Deleting an directory/subdirectory/files
By Java Tip in forum Java TipReplies: 0Last Post: 01-13-2008, 07:18 AM -
Deleting an empty directory
By Java Tip in forum Java TipReplies: 0Last Post: 01-13-2008, 07:17 AM -
Full screen
By Jack in forum Advanced JavaReplies: 2Last Post: 07-02-2007, 05:49 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks