Link to home
Start Free TrialLog in
Avatar of bananaDick
bananaDick

asked on

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();
    }
Avatar of bobbit31
bobbit31
Flag of United States of America image

    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();

     }
Avatar of bananaDick
bananaDick

ASKER

Sorry but I just want to delete the files in the folder, not the folder itself. + why doesn't my code work?
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>happens to return a list of files

should really say

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

;-)
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.
recommendation: points to CEHJ
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