小编mal*_*las的帖子

实现Closeable或实现AutoCloseable

我正在学习Java,我找不到关于implements Closeableimplements 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)

java java-io

113
推荐指数
6
解决办法
10万
查看次数

标签 统计

java ×1

java-io ×1