?
Solved

Deleting files in directory. Why dont this work??

Posted on 2003-02-21
8
Medium Priority
?
271 Views
Last Modified: 2010-03-31


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
Comment
Question by:bananaDick
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 7994585
    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
 

Author Comment

by:bananaDick
ID: 7994627
Sorry but I just want to delete the files in the folder, not the folder itself. + why doesn't my code work?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 180 total points
ID: 7995217
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 86

Expert Comment

by:CEHJ
ID: 7995263
>>happens to return a list of files

should really say

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

;-)
0
 

Expert Comment

by:CleanupPing
ID: 9059161
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
 
LVL 18

Expert Comment

by:bobbit31
ID: 9072047
recommendation: points to CEHJ
0
 
LVL 7

Expert Comment

by:tomboshell
ID: 9582222
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month11 days, 20 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question