模块 java.base
 java.lang

接口 AutoCloseable

所有已知的子接口:
ArenaPREVIEW , AsynchronousByteChannel , AsynchronousChannel , BaseStream<T,S> , ByteChannel , CachedRowSet , CallableStatement , Channel , Clip , Closeable , Connection , DataLine , DirectoryStream<T> , DoubleStream , EventStream , ExecutionControl , ExecutorService , FilteredRowSet , GatheringByteChannel , ImageInputStream , ImageOutputStream , InterruptibleChannel , IntStream , JavaFileManager , JdbcRowSet , JMXConnector , JoinRowSet , Line , LongStream , MidiDevice , MidiDeviceReceiver , MidiDeviceTransmitter , Mixer , ModuleReader , MulticastChannel , NetworkChannel , ObjectInput , ObjectOutput , Port , PreparedStatement , ReadableByteChannel , Receiver , ResultSet , RMIConnection , RowSet , ScatteringByteChannel , ScheduledExecutorService , SecureDirectoryStream<T> , SeekableByteChannel , Sequencer , SourceDataLine , StandardJavaFileManager , Statement , Stream<T> , SyncResolver , Synthesizer , TargetDataLine , Transmitter , WatchService , WebRowSet , WritableByteChannel
所有已知的实现类:
AbstractExecutorService , AbstractInterruptibleChannel , AbstractSelectableChannel , AbstractSelector , AsynchronousFileChannel , AsynchronousServerSocketChannel , AsynchronousSocketChannel , AudioInputStream , BufferedInputStream , BufferedOutputStream , BufferedReader , BufferedWriter , ByteArrayInputStream , ByteArrayOutputStream , CharArrayReader , CharArrayWriter , CheckedInputStream , CheckedOutputStream , CipherInputStream , CipherOutputStream , DatagramChannel , DatagramSocket , DataInputStream , DataOutputStream , DeflaterInputStream , DeflaterOutputStream , DigestInputStream , DigestOutputStream , DirectExecutionControl , FileCacheImageInputStream , FileCacheImageOutputStream , FileChannel , FileImageInputStream , FileImageOutputStream , FileInputStream , FileLock , FileOutputStream , FileReader , FileSystem , FileWriter , FilterInputStream , FilterOutputStream , FilterReader , FilterWriter , ForkJoinPool , Formatter , ForwardingJavaFileManager , GZIPInputStream , GZIPOutputStream , HttpExchange , HttpsExchange , ImageInputStreamImpl , ImageOutputStreamImpl , InflaterInputStream , InflaterOutputStream , InputStream , InputStreamReader , JarFile , JarInputStream , JarOutputStream , JdiDefaultExecutionControl , JdiExecutionControl , JShell , LineNumberInputStream , LineNumberReader , LocalExecutionControl , LogStream , MemoryCacheImageInputStream , MemoryCacheImageOutputStream , MLet , MulticastSocket , ObjectInputStream , ObjectOutputStream , OutputStream , OutputStreamWriter , Pipe.SinkChannel , Pipe.SourceChannel , PipedInputStream , PipedOutputStream , PipedReader , PipedWriter , PrintStream , PrintWriter , PrivateMLet , ProgressMonitorInputStream , PushbackInputStream , PushbackReader , RandomAccessFile , Reader , Recording , RecordingFile , RecordingStream , RemoteExecutionControl , RemoteRecordingStream , RMIConnectionImpl , RMIConnectionImpl_Stub , RMIConnector , RMIIIOPServerImpl , RMIJRMPServerImpl , RMIServerImpl , Scanner , ScheduledThreadPoolExecutor , SctpChannel , SctpMultiChannel , SctpServerChannel , SelectableChannel , Selector , SequenceInputStream , ServerSocket , ServerSocketChannel , Socket , SocketChannel , SSLServerSocket , SSLSocket , StreamingExecutionControl , StringBufferInputStream , StringReader , StringWriter , StructuredTaskScope , StructuredTaskScope.ShutdownOnFailure , StructuredTaskScope.ShutdownOnSuccess , SubmissionPublisher , ThreadPoolExecutor , URLClassLoader , Writer , XMLDecoder , XMLEncoder , ZipFile , ZipInputStream , ZipOutputStream

public interface AutoCloseable
在关闭之前可能持有资源(例如文件或套接字句柄)的对象。 AutoCloseable 对象的 close() 方法在退出 try -with-resources 块时自动调用,该对象已在资源规范标头中声明。这种构造确保及时释放,避免资源耗尽异常和否则可能发生的错误。
API 注意:

基类实现 AutoCloseable 是可能的,事实上是常见的,即使并非其所有子类或实例都将持有可释放资源。对于必须完全通用的代码,或者已知AutoCloseable实例需要资源释放时,建议使用 try-with-resources构造。但是,当使用支持基于 I/O 和非基于 I/O 的形式的设施(例如 Stream )时,使用非基于 I/O 的形式时通常不需要 try -with-resources 块。

自从:
1.7
  • 方法总结

    修饰符和类型
    方法
    描述
    void
    关闭此资源,放弃任何基础资源。
  • 方法详情

    • close

      void close() throws Exception
      关闭此资源,放弃任何基础资源。在 try -with-resources 语句管理的对象上自动调用此方法。
      API 注意:
      虽然此接口方法被声明为抛出 Exception ,但 strongly 鼓励实现者声明 close 方法的具体实现以抛出更具体的异常,或者如果关闭操作不会失败则根本不抛出异常。

      关闭操作可能失败的情况需要实施者特别注意。强烈建议在抛出异常之前放弃底层资源并在内部mark资源关闭。 close 方法不太可能被多次调用,因此可以确保及时释放资源。此外,它减少了当资源包装或被另一个资源包装时可能出现的问题。

      Implementers of this interface are also strongly advised to not have the close method throw InterruptedException . 此异常与线程的中断状态交互,如果 InterruptedException压制,则可能会出现运行时错误行为。更一般地说,如果抑制异常会导致问题,AutoCloseable.close 方法不应抛出它。

      请注意,与 Closeable close 方法不同,此 close 方法需要 not 是幂等的。换句话说,多次调用此 close 方法可能会产生一些可见的副作用,这与 Closeable.close 不同,如果多次调用则要求没有效果。但是,强烈建议此接口的实现者使其 close 方法幂等。

      抛出:
      Exception - 如果此资源无法关闭