Unable to delete a properties file

前端 未结 2 420
轻奢々
轻奢々 2021-01-21 09:04

I have to delete a property file from the path specified. I used the following code:

File f1 = new File(\"C:\\\\Equinox\\\\UIDesign\\\\root\\\\root.properties\")         


        
相关标签:
2条回答
  • 2021-01-21 09:25

    There are a couple of reasons why File.delete() can fail:

    • It's a directory and not empty
    • You don't have the OS permission to delete the file
    • The file is still opened somewhere

    The last one could be your own fault, if you've opened a FileInput/OutputStream for that file and forgot to close it.

    0 讨论(0)
  • 2021-01-21 09:37

    I agree with Michael, his answer makes a lot of sense. Just a comment on your code, you should be doing the following to catch all possible errors and notify the user accordingly:

    try{
     File f1 = new File("C:\\Equinox\\UIDesign\\root\\root.properties"); 
     boolean success=f1.delete();
     if(!success){
        // Notify user that the file 
     }
    catch(SecurityException ex){
     // No sufficient rights to do this operation
    }
    
    0 讨论(0)
提交回复
热议问题