Closing Bufferedreader and System.In

Closing BufferedReader and System.in

Looks like you need:

http://commons.apache.org/io/apidocs/org/apache/commons/io/input/CloseShieldInputStream.html

Wrap that around System.in before making your reader, and then all will be well, since you won't do that when you are using a FileInputStream.

Do I need to close() both FileReader and BufferedReader?

no.

BufferedReader.close()

closes the stream according to javadoc for BufferedReader and InputStreamReader

as well as

FileReader.close()

does.

Closing a BufferedReader

If you are using java 7 or greater and your code is in try catch resource block, then it is Auto closes.

If in below versions you have to close with close(). For that you have to change your current way of using and get the reference.

Use try-with-resources or close this BufferedReader in a finally clause

You are not calling br.close() which means risking a resource leak. In order to reliably close the BufferedReader, you have two options:

using a finally block:

public String loader(String FilePath) {
// initialize the reader with null
BufferedReader br = null;
String str = null;
StringBuilder strb = new StringBuilder();

try {
// really initialize it inside the try block
br = new BufferedReader(new FileReader(FilePath));
while ((str = br.readLine()) != null) {
strb.append(str).append("\n");
}
} catch (FileNotFoundException f) {
System.out.println(FilePath + " does not exist");
return null;
} catch (IOException e) {
e.printStackTrace();
} finally {
// this block will be executed in every case, success or caught exception
if (br != null) {
// again, a resource is involved, so try-catch another time
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

return strb.toString();
}

using a try-with-resources statement:

public String loader(String FilePath) {
String str = null;
StringBuilder strb = new StringBuilder();

// the following line means the try block takes care of closing the resource
try (BufferedReader br = new BufferedReader(new FileReader(FilePath))) {
while ((str = br.readLine()) != null) {
strb.append(str).append("\n");
}
} catch (FileNotFoundException f) {
System.out.println(FilePath + " does not exist");
return null;
} catch (IOException e) {
e.printStackTrace();
}

return strb.toString();
}

closing an buffer reader is compulsory

Always close streams. It's a good habit which helps you to avoid some odd behaviour. Calling close() method also calls flush() so you don't have do this manually.

The best place where to close streams is probably in a finally block. If you have it like in your example and an exception occurs before the in.close() line, the stream won't be closed.

And if you have chained streams, you can only close the last one and all before it are closed too. This means br.close() in your example - not in.close();

Example

try {
// do something with streams
} catch (IOException e) {
// process exception - log, wrap into your runtime, whatever you want to...
} finally {
try {
stream.close();
} catch (IOException e) {
// error - log it at least
}
}

Alternatively you can use closeQuietly(java.io.InputStream) in Apache Commons library.

Is it necessary to close an InputStreamReader in a BufferedReader

There is no problem with doing this, in fact the InputStreamReader documentation from the Java API does it.

In this case, the InputStreamReader is being used by the BufferedReader, which means they will both close when the BufferedReader close() function is called as it: "Closes the stream and releases any system resources associated with it." As the InputStreamReader is also very clearly "associated" with the stream, it too will be closed.

When you build a BufferedReader using an InputStreamReader this way, they really are the same reader bundled together, not two different readers.

BufferedReader is closed when returned from method

You are using try-with-resources:

try (FileInputStream fileInputStream = new FileInputStream(file)) {

This line is creating a FileInputStream which can be used inside your try block. As soon as you leave your try block, the close() method will be called onto the stream. So if you return the stream or its BufferedReader, the stream will already be closed. You should not use try-with-resources or even better, return whatever you need from the stream instead of the stream itself.

Reading from BufferedReader throws exception as stream closed after creating a new BufferedReader

When the BufferedReader gets closed at the end of the try-with-resources block, System.in is also closed and it can't be reopened.

You should open the reader once and keep it open somewhere where you can use it until your program is finished.



Related Topics



Leave a reply



Submit