File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes File input ouput streams Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "File input ouput streams" Watch "File input ouput streams" New topic
Author

File input ouput streams

Hemant Khurana
Greenhorn

Joined: Jan 11, 2007
Posts: 27
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

Joined: Jul 08, 2003
Posts: 24184
    
  34

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 ]

[Jess in Action][AskingGoodQuestions]
Hemant Khurana
Greenhorn

Joined: Jan 11, 2007
Posts: 27
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

Joined: Jul 08, 2003
Posts: 24184
    
  34

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

Joined: Jan 11, 2007
Posts: 27
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File input ouput streams