我正在学习Java,我找不到关于implements Closeable
和implements AutoCloseable
接口的任何好的解释.
当我实现一个时interface Closeable
,我的Eclipse IDE创建了一个方法public void close() throws IOException
.
我可以在pw.close();
没有界面的情况下关闭流.但是,我无法理解如何close()
使用该接口实现该方法.而且,这个界面的目的是什么?
另外我想知道:我怎么检查是否IOstream
真的关闭了?
我正在使用下面的基本代码
import java.io.*;
public class IOtest implements AutoCloseable {
public static void main(String[] args) throws IOException {
File file = new File("C:\\test.txt");
PrintWriter pw = new PrintWriter(file);
System.out.println("file has been created");
pw.println("file has been created");
}
@Override
public void close() throws IOException {
}
Run Code Online (Sandbox Code Playgroud)