• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 275
  • Last Modified:

Deleting files in directory. Why dont this work??



File files = u.getArchive();
String[] dirListing = files.list();

  for(int i = 0;i<dirListing.length;i++)
    {
      File f = new File(dirListing[i]);
      f.delete();
    }
0
bananaDick
Asked:
bananaDick
1 Solution
 
bobbit31Commented:
    public static void deleteFolder(java.io.File delFolder) throws IOException {

          if (delFolder == null || !delFolder.exists() || !delFolder.isDirectory()) {
               throw new IOException ("Specified path is not a valid folder.");
          }

          java.io.File[] files = delFolder.listFiles();

          for (int i=0;i<files.length;i++) {
               if (files[i].isDirectory()) {
                    deleteFolder(files[i]);
               }
               files[i].delete();
          }

          delFolder.delete();

     }
0
 
bananaDickAuthor Commented:
Sorry but I just want to delete the files in the folder, not the folder itself. + why doesn't my code work?
0
 
CEHJCommented:
If the line

>>File files = u.getArchive();

happens to return a list of files that are not in the current directory, it seems to me (i haven't verified it) that your code will simply create new files in the current directory then delete them. Try this:

File files = u.getArchive();
File[] dirListing = files.listFiles();

 for(int i = 0;i<dirListing.length;i++)
   {
     dirListing[i].delete();
   }

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
CEHJCommented:
>>happens to return a list of files

should really say

>>happens to return a directory that's not the current directory

;-)
0
 
CleanupPingCommented:
bananaDick:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
bobbit31Commented:
recommendation: points to CEHJ
0
 
tomboshellCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Accept CEHJ's comment as answer.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Thomas Boshell
EE Cleanup Volunteer
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now