Java的Closeable接口close()方法的意义

Closeable接口又继承了另外的父接口AutoCloseable

其中close()方法是关闭流并且释放与其相关的任何方法,如果流已被关闭,那么调用此方法没有效果。

InputStreamOutputStream类都实现了该接口。

源码:

package java.io;

import java.io.IOException;

/**
 * A {@code Closeable} is a source or destination of data that can be closed.
 * The close method is invoked to release resources that the object is
 * holding (such as open files).
 *
 * @since 1.5
 */
public interface Closeable extends AutoCloseable {

    /**
     * Closes this stream and releases any system resources associated
     * with it. If the stream is already closed then invoking this
     * method has no effect.
     *
     * 

As noted in {@link AutoCloseable#close()}, cases where the * close may fail require careful attention. It is strongly advised * to relinquish the underlying resources and to internally * mark the {@code Closeable} as closed, prior to throwing * the {@code IOException}. * * @throws IOException if an I/O error occurs */ public void close() throws IOException; }

 

你可能感兴趣的:(Java)