Pierre-Henri
Pierre-Henri

Reputation: 1505

Do we necessarily need to close a FileInput/OutputStream to delete a file?

In the application I'm working on we have to deal with temp files. Because of the usual programming mistakes, files might not be disposed properly (until we notice and fix it of course !). I want to write a simple module that will garbage-collect files.

I'm wondering if I can always delete those files, regardless of there is a open stream on it.

I tried it (vm 1.6, linux) and it works, but since I cannot find a specification I'm hesitant to implement it.

Note 1 : we are talking about a single process application.

Note 2 : I'm mainly interested about inputStream, if it makes a difference.

Upvotes: 0

Views: 82

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533870

AFAIK, On Linux you can, on Windows you cannot

As @EJP points out you can get yourself in an endless mess making workarounds. If you are going to work around a bug, only do it because you really have no choice One of the things I have seen with workarounds is you not only get a mess but it can make it much harder to fix bugs later. On more than one occasion I have seen a fix to code breaking a workaround resulting in a break in the program.

Upvotes: 1

Related Questions