- 已知的所有子接口:
-
Arena预览
,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
,HttpClient
,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
,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
对象的try
-with-resources块时,AutoCloseable
对象的close()
方法会自动调用。这种构造确保及时释放资源,避免资源耗尽异常和可能发生的错误。
- API注释:
-
基类实现
AutoCloseable
是可能的,实际上很常见,尽管并非所有子类或实例都将持有可释放的资源。对于必须在完全通用性下运行的代码,或者当已知AutoCloseable
实例需要资源释放时,建议使用try
-with-resources构造。然而,当使用支持基于I/O和非I/O形式的设施(如Stream
)时,在使用非I/O形式时,通常不需要try
-with-resources块。 - 自JDK版本:
- 1.7
-
Method Summary
-
Method Details
-
close
关闭此资源,放弃任何底层资源。此方法会在由try
-with-resources语句管理的对象上自动调用。- API注释:
-
虽然此接口方法声明为抛出
Exception
,但强烈建议实现者声明close
方法的具体实现抛出更具体的异常,或者如果关闭操作不会失败,则根本不抛出异常。关闭操作可能失败的情况需要实现者仔细注意。强烈建议在抛出异常之前放弃底层资源并在内部标记资源为已关闭。
close
方法不太可能被调用多次,因此这确保了资源及时释放。此外,它减少了当资源被另一个资源包装或被包装时可能出现的问题。此接口的实现者还强烈建议不要让
close
方法抛出InterruptedException
。 此异常会影响线程的中断状态,如果InterruptedException
被抑制
,可能会导致运行时错误。更一般地,如果抑制异常会导致问题,AutoCloseable.close
方法不应该抛出它。请注意,与
Closeable
的close
方法不同,此close
方法不需要是幂等的。换句话说,多次调用此close
方法可能会产生一些可见的副作用,与Closeable.close
不同,后者要求多次调用时不产生任何效果。然而,强烈建议此接口的实现者使其close
方法是幂等的。 - 抛出:
-
Exception
- 如果无法关闭此资源
-