divz
divz

Reputation: 7957

Unable to delete a properties file

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"); 
boolean success=f1.delete();

It returns false.

But a text file instead of property file is succesfully deleted.

Upvotes: 3

Views: 2067

Answers (2)

GETah
GETah

Reputation: 21419

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
}

Upvotes: 1

Michael Borgwardt
Michael Borgwardt

Reputation: 346317

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.

Upvotes: 6

Related Questions