java file.delete() 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19871714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 20:55:57  来源:igfitidea点击:

java file.delete() won't work

javafiledelete-file

提问by Gayan Fernando

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.

我创建了一个属性文件,我想加密保留相同文件夹的加密属性文件并删除原始属性文件。当我第一次在我的 Java 应用程序上执行此操作时,它以正确的方式执行。但是当我再次执行此操作时,它不会删除创建的原始属性文件。当我尝试手动删除它时,它给我一条重试消息,说“无法完成操作,因为文件在 java(TM) 平台 SE 二进制文件中打开。关闭文件并重试。” 关闭我的应用程序后,可以手动删除它。我的代码如下。问题出在 propfile123.delete() 上。我该如何解决这个问题。

//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.

采纳答案by Stephen C

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

证据很清楚,删除失败的原因是您的应用程序仍然在某个地方打开了文件。

To resolve this, you need to figure out whereyou 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_Decryptclass, and the way that you are using it. But that's just a guess.)

为了解决这个问题,你需要弄清楚哪里要打开的文件,并确保您关闭它......你试图删除它之前。(我怀疑问题与您的Encrypt_Decrypt课程以及您使用它的方式有关。但这只是猜测。)

回答by Rosendo Ropher

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 BufferedReaderyou can not delete the file.

如果你不给方法的调用close()BufferedReader,你不能删除该文件。

Always close the file before making any changes to it, even delete it or rename it. I hope to help you, greetings.

在对其进行任何更改之前始终关闭该文件,甚至将其删除或重命名。希望能帮到你,问候。