File Delete issue

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

FlyByNtAzAsked:
Who is Participating?
 
CEHJCommented:
Try running the 'handle' utility from sysinternals on those files to see if anything has a lock on them
0
 
CEHJCommented:
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
 
FlyByNtAzAuthor Commented:
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
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
CEHJCommented:
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
 
CEHJCommented:
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
 
FlyByNtAzAuthor Commented:
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
 
CEHJCommented:
Are those zip/jar files in any way involved in your classpath?
0
 
FlyByNtAzAuthor Commented:
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
 
FlyByNtAzAuthor Commented:
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
 
CEHJCommented:
Unfortunately i can't reproduce this at the moment. can you not wait until a bit later to delete them?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.