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方法则不会出现这种情况。

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





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值