Question: I am trying to delete a file using `File.delete()` 

Here is my code:

```
File stagingFile = new File(stagingPath,configFileName);
FileOutputStream fos = new FileOutputStream(stagingFile);
int c = 0;
while((c=input.read())!=-1){
    fos.write(c);
}
fos.flush();
fos.close();
input.close();

```

And after performing some operations, I do this:

```
boolean delete = stagingFile.delete();

```

`delete` returns false. As far as I can see I've closed all handlers relating to `stagingFile`. I'm not sure why it doesn't get deleted

Answer: 
From the [File.delete docs](http://docs.oracle.com/javase/7/docs/api/java/io/File.html#delete%28%29):

> 
> Note that the Files class defines the delete method to throw an IOException when a file cannot be deleted. This is useful for error reporting and to diagnose why a file cannot be deleted.
> 
> 
> 

Give [this method (Files.delete(Path))](http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#delete%28java.nio.file.Path%29) a shot.
Another idea: use [`createTempFile()`](http://docs.oracle.com/javase/7/docs/api/java/io/File.html#createTempFile%28java.lang.String,%20java.lang.String%29) to create a temporary file in the first place. After that, use `Files.delete()` to delete the file.