Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java file.delete() won't work

I have created a properties file and I want to encrypt that keep the same folder encrypted properties file and delete the original properties file. When I do this on my java application first time it does correct way. But when I do this again it doesn't delete the created original properties file. When I try to delete that manually it gives me a try again message saying that "The action can't be completed because the file is open in java(TM) platform SE binary.Close the file and try again." After I closing my application it can be deleted manually. My code is as follows. Problem is on the propfile123.delete(). How can I resolve this problem.

//Encrypt the property file
        Encrypt_Decrypt encrpt= new Encrypt_Decrypt("AES/ECB/PKCS5Padding","properties\\"+name_of_propertice_file+".properties", mstr_pass);
        try {
            encrpt.encrypt();
        } catch (Exception ex) {
            Logger.getLogger(Secure_File.class.getName()).log(Level.SEVERE, null, ex);
        }

        //delete the original properties file
        File propfile123= new File("properties\\"+name_of_propertice_file+".properties");
        System.out.println(propfile123.exists());   // always return true

        System.out.println(propfile123.delete());   //here returns false when I call at second time to this method.
like image 850
Gayan Fernando Avatar asked Nov 09 '13 02:11

Gayan Fernando


1 Answers

The evidence is clear that the reason the delete is failing is that your application still has the file open ... somewhere.

To resolve this, you need to figure out where you are opening the file, and make sure that you close it ... before you attempt to delete it. (I suspect that the problem is something to do with your Encrypt_Decrypt class, and the way that you are using it. But that's just a guess.)

like image 172
Stephen C Avatar answered Sep 18 '22 10:09

Stephen C