On Jul 9, 9:07 am, Philipp <djb...@gmail.com> wrote:
Hello, I use the following code but am not sure that it is the best or
correct way to close the stream. In particular, if url.openStream()
succeeds I have an open stream, but if any of the two other
constructors throws an exception, reader will be null and the stream
will remain open even after the finally block.
public class WhatToClose {
public static void main(String[] args) {
BufferedReader reader = null;
There's another idiom that skips the 'null' assignment - see below.
try{
URL url = new URL("http://www.yahoo.com/");
reader = new BufferedReader(new InputStreamReader(url.openStream
()));
// use reader here
String line = null;
No need to superfluously assign a spurious 'null' to 'line'.
while (( line = reader.readLine()) != null){
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(reader != null){
try{
reader.close();
You could just trap and close the URL stream rather than the reader.
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
I could allocate three refs (see below) buts that's really a PITA. How
do you do it?
public static void main(String[] args) {
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
This should not be necessary.
One way is to chain the I/O classes in separate try-catch blocks, so
that a later nullity check is unnecessary; you can use 'assert'
statements instead. The result is verbose but precise control over
what happens and how it's logged, and the safety of assertably
initialized streams and readers.
public static void main( String [] args )
{
final InputStream is;
try
{
is = url.openStream();
}
catch ( IOException exc )
{
logger.error( "Cannot open URL stream. " + exc.getMessage() );
return;
}
assert is != null;
final BufferedReader br;
try
{
br = new BufferedReader( new InputStreamReader( is ));
}
catch ( IOException exc )
{
logger.error( "Cannot open URL stream reader. " + exc.getMessage
() );
try
{
is.close();
}
catch ( IOException closex )
{
logger.error( "Cannot close URL stream. " + closex.getMessage
() );
}
return;
}
assert br != null;
try
{
for ( String line; (line = br.readLine()) != null; )
{
System.out.println(line);
}
}
catch ( IOException exc )
{
logger.error( "Cannot read URL stream reader. " + exc.getMessage
() );
}
finally
{
try
{
br.close();
}
catch ( IOException closex )
{
logger.error( "Cannot close URL stream reader. " +
closex.getMessage() );
}
}
}
--
Lew
the for loop for reading the lines. The return in the catch block is
pretty nifty too.
constructor will succeed. So closing the BufferedReader will suffice.