I'm trying to delete a file, after writing something in it, with FileOutputStream
. This is the code I use for writing:
private void writeContent(File file, String fileContent) {
FileOutputStream to;
try {
to = new FileOutputStream(file);
to.write(fileContent.getBytes());
to.flush();
to.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
As it is seen, I flush and close the stream, but when I try to delete, file.delete()
returns false.
I checked before deletion to see if the file exists, and: file.exists()
, file.canRead()
, file.canWrite()
, file.canExecute()
all return true. Just after calling these methods I try file.delete()
and returns false.
Is there anything I've done wrong?