Link to home
Start Free TrialLog in
Avatar of FlyByNtAz
FlyByNtAz

asked on

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

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of FlyByNtAz
FlyByNtAz

ASKER

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
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

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

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

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