MarkHammons
MarkHammons

Reputation: 113

What happens to a File accessed by a FileInputStream if the program closes without the stream closing?

If I open a FileInputStream on File A, and the program terminates without calling close() method. Will A be left with a file access handle open or in any other messy state or is it fine?

Upvotes: 3

Views: 104

Answers (3)

Absent
Absent

Reputation: 914

Usually the garbage collector will clean it up for you at the end of the execution. However it is a very bad practice to not free the resources you are using.

Well, many classes such as FileInputStream and RandomAccessFile are written with a finalize() method which ensures that IF an instance in garbage collected, close() will be called first. So in many cases, garbage collection does indirectly free up files, and it is often possible for programmers to be lazy about closing resources, because garbage collection usually cleans them up for you. Unfortunately.

-Discussion about InputStream and Garbage Collection

Upvotes: 2

Lior Alon
Lior Alon

Reputation: 183

Look here (it's in C# but it is the same): What happens to an open Filestream if it is not close due to program crash?

From my experience - the connection closes on its own.

Notice that it is a very bad practice not to close on your own - your will waste your system resources

Upvotes: 1

Geek
Geek

Reputation: 23419

The File Handle is released by the OS once the process which opened it dies.

Upvotes: 2

Related Questions