Unclosed File input stream , Any performance implications?

Hi,

I open a file input stream as below and read the contents of a file and exit from the method without closing the input stream [fin.close()].

1) What are the implications of not closing input stream, performance related?.

2) Since fin is a local variable once the method terminates the variable will be destroyed, In this do i still need to close the input stream?

FileInputStream fin = new FileInputStream(file);

Please advice.

Thanks

Deepak

[496 byte] By [deepu-jaina] at [2007-11-27 11:29:47]
# 1

Yes , it's always better to close stream object. It canl affect /corrupt file operations.

Thanks

Nicky

LaKshya_Nickya at 2007-7-29 16:30:03 > top of Java-index,Java Essentials,Java Programming...
# 2

I want to know what excatly will happen when i dont close.

I did a google and saw it will result in file descriptor leak, But i was not able to find "What is file descriptor leak?" , Also i want to know any other potential performance issues with this?

Please reply.

deepu-jaina at 2007-7-29 16:30:03 > top of Java-index,Java Essentials,Java Programming...
# 3

It might lock the file.

Also, I accidentally forgot to close a writer the other day at work

and it caused a bug that drove me crazy. Because I never closed it

the write buffer never flushed() out completely.

TuringPesta at 2007-7-29 16:30:03 > top of Java-index,Java Essentials,Java Programming...
# 4

What is this question about exactly? Why on earth would you not want to close the input stream once you know that the operation exists and causes problelms if it wasn't executed?

Just do it.

ejpa at 2007-7-29 16:30:03 > top of Java-index,Java Essentials,Java Programming...
# 5

Hi,

It was just a question , I wanted to know the implications of not closing.

1) Will below code lock the file and then not closing the connection will cause a error?

2) Does opening the file in read only mode cause the file to be locked so that it cannot be opened for reading or writing later?

FileInputStream fin = new FileInputStream(file);

Java doc:

Creates a FileInputStream by opening a connection to an actual file, the file named by the path name name in the file system. A new FileDescriptor object is created to represent this file connection.

deepu-jaina at 2007-7-29 16:30:03 > top of Java-index,Java Essentials,Java Programming...
# 6

> Hi,

> It was just a question , I wanted to know the

> implications of not closing.

>

> 1) Will below code lock the file and then not closing

> the connection will cause a error?

Trying to open the file can cause an error if there are too many open file descriptors.

> 2) Does opening the file in read only mode cause the

> file to be locked so that it cannot be opened for

> reading or writing later?

Probably not, but it can still cause an error, see above.

Kaj

kajbja at 2007-7-29 16:30:03 > top of Java-index,Java Essentials,Java Programming...