TheNotMe
TheNotMe

Reputation: 1058

Java closing BufferedReader, Scanner and FileReader

In my program, I parse files. I open them like this:

Scanner s = new Scanner(new BufferedReader(new FileReader("file1.txt")));

and close it like this:

s.close();

My question is: when I do s.close() does this also close the FileReader and the BufferedReader too?

Upvotes: 0

Views: 805

Answers (1)

someone
someone

Reputation: 6572

Java doc say

public void close()

Closes this scanner.

If this scanner has not yet been closed then if its underlying readable also implements the Closeable interface then the readable's close method will be invoked. If this scanner is already closed then invoking this method will have no effect.

Attempting to perform search operations after a scanner has been closed will result in an IllegalStateException. 

Since BufferedReader has implemented Closeable and it will invoke buffer reader close method.

Upvotes: 2

Related Questions