• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

I/O:need close?

 
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code creating a stream first then, start to read from
the stream. If it is going wrong for reading. it is going to close
the stream and return false(as line 18). My question is in line 8, if
FileNotFoundException is raised, should I close the stream(b.close()),before return?
// create a stream
try{
FileReader f = new FileReader"text.txt");
BufferedReader b = new BufferedReader(f);
}
catch(FileNotFoundException e){
System.out.println("FileNotFoundException "+e);
//line 8b.close();
return false;
}

//read first line
try{
str = b.readLIne();
}
catch(IOExcetion e){
System.out.println("IOException "+e);
b.close();//line18
return false;
}
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nan,
You'll get an error if you try to close b in the catch blocks as it won't be visible It might be better to put the close() in a finally

Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Jane Griscti (edited February 18, 2001).]
 
nan sh
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jane.
reply
    Bookmark Topic Watch Topic
  • New Topic