在我的程序中,我解析文件。我这样打开它们:
Scanner s = new Scanner(new BufferedReader(new FileReader("file1.txt")));
并像这样关闭它:
s.close();
我的问题是:当我这样做时,s.close()
是否也会关闭 FileReader 和 BufferedReader?
在我的程序中,我解析文件。我这样打开它们:
Scanner s = new Scanner(new BufferedReader(new FileReader("file1.txt")));
并像这样关闭它:
s.close();
我的问题是:当我这样做时,s.close()
是否也会关闭 FileReader 和 BufferedReader?
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
,它将调用缓冲区读取器关闭方法。