Rachel
Rachel

Reputation: 13

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

Upvotes: 0

Views: 1813

Answers (1)

Thomas
Thomas

Reputation: 88707

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.

Upvotes: 1

Related Questions