简体   繁体   English

java file.delete()不起作用

[英]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. 当我第一次在我的java应用程序上执行此操作时,它确实正确。 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." 当我尝试手动删除它时,它再次尝试消息,说“由于文件在java(TM)平台SE二进制文件中打开,因此操作无法完成。关闭文件并重试。” After I closing my application it can be deleted manually. 关闭我的应用程序后,可以手动删除它。 My code is as follows. 我的代码如下。 Problem is on the propfile123.delete(). 问题在于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.

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.) (我怀疑这个问题与你的Encrypt_Decrypt类以及你使用它的方式有关。但这只是猜测。)

When you open a file: 当您打开文件时:

BufferedReader br = new BufferedReader (new FileReader (new File ("somefile"))); 

if you don´t make a call to the method close() of BufferedReader you can not delete the file. 如果您没有调用BufferedReader close()方法,则无法删除该文件。

Always close the file before making any changes to it, even delete it or rename it. 始终在对文件进行任何更改之前关闭该文件,甚至将其删除或重命名。 I hope to help you, greetings. 我希望能帮助你,问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM