P
Philipp
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;
try{
URL url = new URL("http://www.yahoo.com/");
reader = new BufferedReader(new InputStreamReader(url.openStream
()));
// use reader here
String line = null;
while (( line = reader.readLine()) != null){
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(reader != null){
try{
reader.close();
} 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;
try{
URL url = new URL("http://www.yahoo.com/");
is = url.openStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
String line = null;
while (( line = br.readLine()) != null){
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(br != null){
try{
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(isr != null){
try{
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is != null){
try{
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Thanks Phil
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;
try{
URL url = new URL("http://www.yahoo.com/");
reader = new BufferedReader(new InputStreamReader(url.openStream
()));
// use reader here
String line = null;
while (( line = reader.readLine()) != null){
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(reader != null){
try{
reader.close();
} 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;
try{
URL url = new URL("http://www.yahoo.com/");
is = url.openStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
String line = null;
while (( line = br.readLine()) != null){
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(br != null){
try{
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(isr != null){
try{
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is != null){
try{
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Thanks Phil