java IO相关API探索之 AutoCloseable接口

从今天开始,要每天都坚持写一篇博客.每天都学习一点新知识,直到能够把java的API全部搞熟,搞透..量变引起质变,坚持下去总会有效果的.翻开jdk API,首先看到的是applet得API,因为只是在大学期间看过applet,其他时间就没看过了,所以这次就跳过applet部分,直接进入java的经常用到的部分.

IO操作基本上在写任何程序的时候都会用到,包括servlet或者android以及使用SSH框架的时候,在我们打开一个文件流的时候,要在程序的最后使用close方法将文件流或者其他一些打开的资源关闭,而这个close方法就是由AutoCloseable接口传递给子类或者子接口的.这个接口比较简单,只定义了一个close()接口。

整个接口文件代码比较少,下面是代码部分以及注释:

public interface AutoCloseable {
    /**
     * Closes this resource, relinquishing any underlying resources.
     * This method is invoked automatically on objects managed by the
     * {@code try}-with-resources statement.
     *
     * <p>While this interface method is declared to throw {@code
     * Exception}, implementers are <em>strongly</em> encouraged to
     * declare concrete implementations of the {@code close} method to
     * throw more specific exceptions, or to throw no exception at all
     * if the close operation cannot fail.
     *
     * <p> Cases where the close operation may fail require careful
     * attention by implementers. It is strongly advised to relinquish
     * the underlying resources and to internally <em>mark</em> the
     * resource as closed, prior to throwing the exception. The {@code
     * close} method is unlikely to be invoked more than once and so
     * this ensures that the resources are released in a timely manner.
     * Furthermore it reduces problems that could arise when the resource
     * wraps, or is wrapped, by another resource.
     *
     * <p><em>Implementers of this interface are also strongly advised
     * to not have the {@code close} method throw {@link
     * InterruptedException}.</em>
     *
     * This exception interacts with a thread's interrupted status,
     * and runtime misbehavior is likely to occur if an {@code
     * InterruptedException} is {@linkplain Throwable#addSuppressed
     * suppressed}.
     *
     * More generally, if it would cause problems for an
     * exception to be suppressed, the {@code AutoCloseable.close}
     * method should not throw it.
     *
     * <p>Note that unlike the {@link java.io.Closeable#close close}
     * method of {@link java.io.Closeable}, this {@code close} method
     * is <em>not</em> required to be idempotent.  In other words,
     * calling this {@code close} method more than once may have some
     * visible side effect, unlike {@code Closeable.close} which is
     * required to have no effect if called more than once.
     *
     * However, implementers of this interface are strongly encouraged
     * to make their {@code close} methods idempotent.
     *
     * @throws Exception if this resource cannot be closed
     */
    void close() throws Exception;
}
大概翻译了一下上面关于该类以及方法的解释:

关闭这个资源,放弃任何潜在的资源。调用该方法能够自动管理在try代码块中声明的资源。这个close方法抛出了Exception异常,但是强烈建议它的实现类能够抛出更加具体的异常信息,当资源能够正确关闭的时候不抛出任何异常。实现类需要更加关注 关闭操作失败的场景。强烈建议在放弃底层和相关资源并把资源标记为关闭之前抛出异常。这个方法可能不止被调用一次以确保能够及时的释放资源。此外,这个方法减少了当资源封装或者被其他资源封装时出现的问题。同时也不建议实现类在该类上抛出InterruptedException异常,因为InterruptedException异常在线程中断或者运行时可能出现,该InterruptedException异常与线程的中断状态和运行时的不当行为的相互作用很可能发生,造成线程或者运行时异常被抑制,通常如果它造成了某个Exception遭到抑制,那么这个方法就不应该抛出这种异常。不止一次的调用这个方法可能造成一些负面影响,而调用Closeable中的close方法则不会出现这种情况。

这段话翻译的我感觉很多地方都不太懂。希望理解这段话的人能够更加深刻的解释一下.欢迎指导批评..





你可能感兴趣的:(java IO相关API探索之 AutoCloseable接口)