Ghost files

Halloween has passed but I’ve been experiencing ghost files. I am trying to delete a file after reading it, but it gave me AccessDeniedException.
Here are the symptoms:

file.delete() returns false
file.exists() returns false

dir.listFiles(…) -> I see the file
in Windows Explorer -> I see the file

when I try to open the file in notepad++ – it says it doesn’t exists and asks if I want to create it — I click yes — and it says it cannot be created.

After some investigation, I have solved the mystery.

I had

turns out the stream wasn’t closed. So it was holding on to the file and kept it in a weird state.

So when I change it to

try(Stream<String> lines = Files.lines(Paths.get(filename))) {

The stream closes on exit, and I no longer get AccessDeniedException when I try to delete the file.

Although the Stream from Files gave me some hiccups (including not mockable when testing in Mockito because it is static), I still love it for its convenience.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s