Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File input ouput streams

 
Hemant Khurana
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please look at the code snippet below

As you can notice i have created BufferedReader object in using inStream object which is being passed to load method.I am closing the inStream object in finally block of methodA but i am not closing the in object created in method load.

Now my question is does the in object gets closed automatically when inStream object is closed as it was created through instrean object?

[ Added code tags - Jim ]
[ January 12, 2007: Message edited by: Jim Yingst ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

No, it won't be closed. You may still be able to read previously buffered data from it; then when the buffer empties out, you'll get an IOException.

[ EJFH: Editing my earlier incorrect answer, in which I misinterpreted the question. Thanks, Jim! ]
[ January 12, 2007: Message edited by: Ernest Friedman-Hill ]
 
Hemant Khurana
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
i was doing some of my own search on this problem when i realized that in object is of type BufferedReader.

Correct me if i am wrong! A buffred reader will use the input stream passed to it(as instream in the example) for reading and wont create a new stream of its own. so do i really need to call close() method on in object?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Technically, no; it won't hurt anything if you don't. Stylistically, it's a problem: a maintenance programmer might very well come along and wonder why the close is missing, and add it, possibly breaking something else.

Why do you need to do things this way?
 
Hemant Khurana
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you guessed it right..!!!
I am currently into maintainace activity and wondering why this stream has not been closed.
The reason why i am asking this question is that my application is throwing
ioException.FileNotFound(Too many open files). The exception are coming when ever a reading or writting function is being performed in the system.
System is throwing ioException.FileNotFound(Too many open files) for the files even which are present at the desired location and available for reading.
The load method is used very frequently and belive could be one of the reasons for (Too many open files) as the stream is not being closed and the system runs out of streams. Any idea about what could be the reason for the given problem.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic