为什么java.io.File没有close方法? [英] Why doesn't java.io.File have a close method?
问题描述
java.io.RandomAccessFile
确实具有 close()
方法,而 java.io.File
没有.这是为什么?该文件在完成后会自动关闭吗?
While java.io.RandomAccessFile
does have a close()
method java.io.File
doesn't. Why is that? Is the file closed automatically on finalization or something?
推荐答案
File
类的javadoc将此类描述为:
The javadoc of the File
class describes the class as:
文件和目录路径名的抽象表示.
An abstract representation of file and directory pathnames.
File
只是路径名的一种表示形式,带有一些与文件系统有关的方法(例如 exists()
)和目录处理,但是实际的流输入和输出已完成别处.流可以打开和关闭,文件不能.
File
is only a representation of a pathname, with a few methods concerning the filesystem (like exists()
) and directory handling but actual streaming input and output is done elsewhere. Streams can be opened and closed, files cannot.
(我个人很不幸,Sun随后创建了 RandomAccessFile
,这给命名不一致带来了很大的困惑.)
(My personal opinion is that it's rather unfortunate that Sun then went on to create RandomAccessFile
, causing much confusion with its inconsistent naming.)
这篇关于为什么java.io.File没有close方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!