Delete file ending with .csv, .html, .doc in java
I want to delete a file ending with .csv,.html,.doc.. I have to upload a file to the remote开发者_运维百科 location. After uploading the file, it should be deleted form the localpath. I have written code which is working perfectly for one function but not for another function. My code snippet:
localfilePath="D:/archives/shai/aaabrowser.csv";
if (isFileTransferComplete == false){
error ="SFTP failure";
if(isFileTobeDeleted){
File target = new File(localfilePath);
target.delete();
}
}
please help
There might still be an open file handle that prevents the file from being deleted. Sometime the JVM holds those handles and the file can be deleted once the JVM is shut down (there is a deleteOnExit()
method).
The delete()
method returns a boolean to indicate success or failure, so check that.
Alternatively, try Apache Commons FileIO which has a method FileUtils.forceDelete( file )
, that throws an exception when it fails.
精彩评论