• Post Reply Bookmark Topic Watch Topic
  • New Topic

java i/o stream close() how it works?  RSS feed

 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there

I have a question that

for every stream that we create for java i/o we write at the end in finally block



it works

my question is as per syntax in if (is!=null) this means that if is means stream contain some value then close it(!=null)

then how it in my opnion it should be like (in logical way)


if stream contain nothing then close it

if(is==null)
{
is.close();
}

I am confuse about working this line

can any one explain how it works

thanks
 
Scott Winterbourne
Ranch Hand
Posts: 116
2
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your example, you have assigned the input stream to the variable "is". If the variable "is" is NULL, that means it does not point to an input stream object. Therefor if you try to call a method on a variable that does not point to an object, you will get a NullPointerException.

This is the reason to test if the variable is NULL before trying to call a method on it. An exception may have been thrown while the input stream was being created so the input stream object was never assigned to the "is" variable.



Hope this helps.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Scott

Great Answars clearly understand
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I told you in one of your other threads, you are better off learning about try with resources. Then you don't need to write all that boilerplate code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!