Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How BufferedReader.close() closes the input stream  RSS feed

 
Arka Sharma
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi All,
I have one query how BufferedReader and it's counterparts (like Byte streams and Writer) closes the stream? Say I have the following code


Now will that "close()" method close the internal filereader stream.On the other hand if we replace that call with "fileReader.close()" what will happen ?

Regards,
Arka
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, calling close() will chain the close call to the enclosed reader. The purpose of close() is to free any resources associated with the reader, like file pointers for example. Closing the underlying FileReader will release the file pointer, but not release any resources held by the BufferedReader. The BufferedReader, however, probably doesn't have any resources to free (I'd have to look at the source code to be sure) so it really wouldn't be a problem that it didn't get closed, but it is bad style.

Speaking of bad style, it's "its counterparts", not "it's".
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!