m/RandomAccessFile,就要把他关联的文件描述符所关联的所有FileInputStream/FileOutputStream/RandomAccessFile
对象都关闭呢?
这个可以看看FileInputStream#close
的JavaDoc:
Closes this file input stream and releases any system resources
associated with the stream.
If this stream has an associated channel then the channel is closed
as well.
也就是说FileInputStream#close
是会吧输入/出流对应的系统资源关闭的,也就是输入/出流对应的文件描述符会被关闭,而如果这个文件描述符还关联这其他输入/出流,如果文件描述符都被关闭了,这些流自然也就不能用了,所以closeAll里把这些关联的流通通都关闭掉,使其不再可用。
总结
FileDescriptor
的作用是保存操作系统中的文件描述符
FileDescriptor
实例会被FileInputStream/FileOutputStream/RandomAccessFile
持有,这三个类在打开文件时,在JNI代码中使用open
系统调用打开文件,得到文件描述符在JNI代码中设置到FileDescriptor
的fd
成员变量上
- 关闭
FileInputStream/FileOutputStream/RandomAccessFile
时,会关闭底层对应的文件描述符,如果此文件描述符被多个FileInputStream/FileOutputStream/RandomAccessFile
对象持有,则这些对象都会被关闭。关闭是文件底层是通过调用close
系统调用实现的。
参考资料