I am really disappointed to discuss a similar java 6 file deletion issue and not being able to find a solution on these similar posts on flie deletion Post1 and Post2.
I am working on an application which downloads a file from the FTP Server.This downloading is achieved by obtaining the stream , reading from this stream and writing it to a file RandomAccessFile
. In case of corrupt downloads,I wish to delete the file on the disk.
I am unable to delete the file manually or via the application.It is very obvious that some file handler still has the lock for the file due to which file deletion is a failure. However,I cant understand which file handlers possesses the file lock as I have made sure that I close all the file and the stream objects.
Finally,the code snippet
public class OprDownload extends Observable implements Runnable
{
public void run()
{
//Code to connect to ftp,obtain the stream and write to a file
if (download complete)
{
if(mwwObjFile!=null)
{
mwwObjFile.close();
}
if(stream!=null)
{
stream.close();
}
if(compareChecksum())//If checksum match success
{
updateDownloadDetails();
cwwIntStatus = COMPLETE;
}
else
{
cwwIntStatus = CHECKSUM_FAIL;
deleteCorruptUpdateFromDownloadsFolder();
}
}
}
public void deleteCorruptUpdateFromDownloadsFolder()
{
String fileOndisk = "FileName";
File mwwFileToBeDeleted = new File(fileOndisk );
if(mwwFileToBeDeleted .exists())
{
System.out.println("File Deleted"+mwwFileToBeDeleted .delete());
}
}
}