在try( ...)⾥声 明的资源,会在try-catch代码块结束后⾃动关闭掉。
注意点
- 实现了AutoCloseable接⼝的类,在try()⾥声明该类实例的时候,try结束后⾃动调⽤的 close⽅法,这个动作会早于finally⾥调⽤的⽅法
- 不管是否出现异常,try()⾥的实例都会被调⽤close⽅法
- try⾥⾯可以声明多个⾃动关闭的对象,越早声明的对象,会越晚被close掉
// try-with-resources
try(ServletOutputStream outputStream = response.getOutputStream()) {
/*ServletOutputStream outputStream = response.getOutputStream()*/
ImageIO.write(image, "jpg", outputStream);
outputStream.flush();
/*outputStream.close();*/
} catch (IOException e) {
log.error("获取流出错:{}", e.getMessage());
}
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
*/
/*关闭此资源,放弃任何底层资源。在try-with-resources语句管理的对象上自动调用此方法。虽然这
个接口方法被声明为抛出Exception,但强烈鼓励实现者声明关闭方法的具体实现来抛出更具体的异
常,或者如果关闭操作不能失败,则完全不抛出异常。关闭操作可能失败的情况需要实现者仔细注意。
强烈建议在抛出异常之前放弃底层资源,并在内部将资源标记为关闭。close方法不太可能被多次调用,
因此这确保了资源被及时释放。此外,它还减少了资源被另一个资源包装或被包装时可能出现的问题。
强烈建议此接口的实现者不要使用close方法抛出InterruptedException。此异常与线程的中断状态交
互,如果InterruptedException被抑制,则很可能发生运行时不当行为。更一般地说,如果它会导致
被抑制的异常出现问题,则使用AutoCloseable。Close方法不应该抛出它。注意,与java.io的关闭方
法不同。可封闭的,这个封闭方法不需要幂等。换句话说,与Closeable不同,多次调用这个close方法
可能会产生一些可见的副作用。关闭,如果多次调用则要求没有效果。但是,强烈建议这个接口的实现
者使他们的close方法幂等。抛出:异常—如果不能关闭此资源*/
void close() throws Exception;
}