Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method BufferedReader.close() affect on InputStream?

 
Ivan Franko
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For example, I have next method getReader:



and example how I use this method:



In second part of code I use method close() for object reader, but I dont do anything with InputStream in the end. It is ok? Or I must close InputStream too?

Thanks.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calling close() on the BufferedReader will also close the underlying InputStream that the BufferedReader is reading from. You don't need to explicitly close the InputStream yourself.
 
Ivan Franko
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong, do you know where I can found confirmation in official docs?
 
Mike Simmons
Ranch Hand
Posts: 3040
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I know, the docs are vague about this. Close() "Closes the stream and releases any system resources associated with it." It's not really clear what "any system resources" means, and it's not clear why they never explicitly name the specific things that do get "released". But in practice, close() on BufferedReader does indeed call close() on the underlying stream. This can be verified by looking at the source code, if you like. The same is true for other similar IO classes that wrap some other underlying stream - OutputStreamWriter, FilterReader, etc. They all close the underlying stream, even though none of them properly document it.
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Franko wrote:Jesper de Jong, do you know where I can found confirmation in official docs?

In the source code of java.io.BufferedReader

Well, I'm not joking - you can actually go and look into source code of those classes - that is far more helpful especially when something is not documented.



I hope this helps.
 
Stanley Mungai
Ranch Hand
Posts: 155
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan: I have had a problem of not closing Input streams before. I can tell you that they are safer closed - explicitly.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic