?
Solved

File Delete issue

Posted on 2008-11-18
11
Medium Priority
?
724 Views
Last Modified: 2012-05-05
I cant seem to get a file deleted no matter how I try using the JAVA File class.  I have made sure that all streams and other potential programtic references to the file have been closed and nulified.  I can remove these files manually using windows explorer so I would think there are no outstanding file locks or sharing violations etc.  I have tried all manner of things and the code snippet below is what I have implemented as of this time.
File myFile = null;
File myDirFile = null;
String[] filesToRemove;
 
myDirFile = new File(this.sBasePath);
filesToRemove = myDirFile.list();
for (int i=0; i < filesToRemove.length; i++) {
     if (filesToRemove[i].toUpperCase().contains(this.sName.toUpperCase()) == true) {
          myFile = new File(myDirFile + this.sFileSeparator,  filesToRemove[i]);
          if (myFile.delete() == true)
              System.out.println("Deleted file: " + filesToRemove[i]);
           else
               System.out.println("Failed to delete file: " + filesToRemove[i]);
       }
}

Open in new window

0
Comment
Question by:FlyByNtAz
  • 6
  • 4
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 22989746
What's your debug output saying?

You can simplify your code (and make it less error prone) by doing

File[] filesToRemove = myDirFile.listFiles();

then you don't need to construct files from path fragments
0
 

Author Comment

by:FlyByNtAz
ID: 22989795
Below is the console output from the code snippet in the problem statement:

Failed to delete file: SMMA_LO_00001.jar
Failed to delete file: smma_lo_00001.zip
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 22989868
Can you change

if (myFile.delete() == true)

into the following and then report back?
if (myFile.exists()) {
	System.out.printf("File %s exists, so attempting to delete it\", myFile);
}
else {
	System.err.printf("File %s does NOT exist, so skipping it\", myFile);
	continue;
}
if (myFile.delete() == true)

Open in new window

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!

 
LVL 86

Expert Comment

by:CEHJ
ID: 22989880
Oops, sorry
if (myFile.exists()) {
	System.out.printf("File %s exists, so attempting to delete it\n", myFile);
}
else {
	System.err.printf("File %s does NOT exist, so skipping it\n", myFile);
	continue;
}
if (myFile.delete() == true)

Open in new window

0
 

Author Comment

by:FlyByNtAz
ID: 22989971
Modified as per your two suggestions w/o any difference in behavior.

The following is the console output:
File: C:\DOCUME~1\Delema\LOCALS~1\Temp\math\SMMA_LO_00001.jar exists, so attempting to delete it
Failed to delete file: C:\DOCUME~1\Delema\LOCALS~1\Temp\math\SMMA_LO_00001.jar
File: C:\DOCUME~1\Delema\LOCALS~1\Temp\math\smma_lo_00001.zip exists, so attempting to delete it
Failed to delete file: C:\DOCUME~1\Delema\LOCALS~1\Temp\math\smma_lo_00001.zip

File myDirFile = null;
File[] filesToRemove;
 
myDirFile = new File(this.sBasePath);
filesToRemove = myDirFile.listFiles();
for (int i=0; i < filesToRemove.length; i++) {
    if (filesToRemove[i].getName().toUpperCase().contains(this.sName.toUpperCase()) == true) {
        if (filesToRemove[i].exists()) {
            System.out.printf("File: %s exists, so attempting to delete it\n", filesToRemove[i]);
        }
        else {
            System.out.printf("File: %s does NOT exist, so skipping it\n", filesToRemove[i]);
            continue;
        }
        if (filesToRemove[i].delete() == true)
          System.out.println("Deleted file: " + filesToRemove[i]);
       else
           System.out.println("Failed to delete file: " + filesToRemove[i]);
    }
}

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 22990010
Are those zip/jar files in any way involved in your classpath?
0
 

Author Comment

by:FlyByNtAz
ID: 22990196
No, they are extracted as resources from a JAR file that is used as a container much like an image file that you would find in a JAR file.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 22990469
Try running the 'handle' utility from sysinternals on those files to see if anything has a lock on them
0
 

Author Comment

by:FlyByNtAz
ID: 22996459
As you expected there seem to be dangling file handle references hanging around as seen by the following output from the handle utility you mentioned:

------------------------------------------------------------------------------
java.exe pid: 5956 NCSP\Delema
    C: File  (RW-)   C:\MATH
--->>>  3ACC: File  (RW-)   C:\DOCUME~1\Delema\LOCALS~1\Temp\math\SMMA_LO_00001.jar
--->>> 3AE4: File  (RW-)   C:\Documents and Settings\Delema\Local Settings\Temp\math\smma_lo_00001.zip
 3AEC: File  (RW-)   C:\MATH\JARS\Deploy\WindowsRTE.jar
 3AF4: File  (RW-)   C:\MATH\JARS\Deploy\SMMA_LO_00004.jar
 3AFC: File  (RW-)   C:\MATH\JARS\Deploy\SMMA_LO_00003.jar
 3B04: File  (RW-)   C:\MATH\JARS\Deploy\SMMA_LO_00002.jar
 3B0C: File  (RW-)   C:\MATH\JARS\Deploy\SMMA_LO_00001.jar
 3B14: File  (RW-)   C:\MATH\JARS\Deploy\ProgressReport.jar
 3B40: File  (RW-)   C:\DOCUME~1\Delema\LOCALS~1\Temp\Manifest.jar
 3B48: File  (RW-)   C:\MATH\JARS\Deploy\Manifest.jar
 3B50: File  (RW-)   C:\MATH\JARS\Deploy\MacRTE.jar
 3B58: File  (RW-)   C:\MATH\JARS\Deploy\Common.jar
 3B70: File  (RW-)   C:\Program Files\Java\jdk1.5.0_14\jre\lib\ext\sunpkcs11.jar
 3B78: File  (RW-)   C:\Program Files\Java\jdk1.5.0_14\jre\lib\ext\sunjce_provider.jar
 3B84: File  (RW-)   C:\Program Files\Java\jdk1.5.0_14\jre\lib\ext\localedata.jar
 3B8C: File  (RW-)   C:\Program Files\Java\jdk1.5.0_14\jre\lib\ext\dnsns.jar
 3E4C: File  (RW-)   C:\Program Files\Java\jdk1.5.0_14\jre\lib\charsets.jar
 3E54: File  (RW-)   C:\Program Files\Java\jdk1.5.0_14\jre\lib\jce.jar
 3E5C: File  (RW-)   C:\Program Files\Java\jdk1.5.0_14\jre\lib\jsse.jar
 3E64: File  (RW-)   C:\Program Files\Java\jdk1.5.0_14\jre\lib\rt.jar
 3E88: Section       \BaseNamedObjects\hsperfdata_Delema_5956
 3E8C: File  (R-D)   C:\DOCUME~1\Delema\LOCALS~1\Temp\hsperfdata_Delema\5956
 3FD0: Section       \BaseNamedObjects\ShimSharedMemory

C:\Documents and Settings\Delema\Desktop>
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 22998428
Unfortunately i can't reproduce this at the moment. can you not wait until a bit later to delete them?
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

839 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