我在学习 Java 的过程中,我找不到任何关于 implements Closeable和 implements AutoCloseable接口的好的解释。
当我实现一个 interface Closeable时,我的 EclipseIDE 创建了一个方法 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 {
}