0

在我的程序中,我解析文件。我这样打开它们:

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

并像这样关闭它:

s.close();

我的问题是:当我这样做时,s.close()是否也会关闭 FileReader 和 BufferedReader?

4

1 回答 1

2

Java 文档说

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. 

由于BufferedReader已经实现Closeable,它将调用缓冲区读取器关闭方法。

于 2012-12-23T10:55:52.827 回答