Why doesn't java.io.File have a close method?

后端 未结 5 2008
不知归路
不知归路 2020-11-29 23:53

While java.io.RandomAccessFile does have a close() method java.io.File doesn\'t. Why is that? Is the file closed automatically on fina

5条回答
  •  失恋的感觉
    2020-11-29 23:57

    The javadoc of the File class describes the class as:

    An abstract representation of file and directory pathnames.

    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.

    (My personal opinion is that it's rather unfortunate that Sun then went on to create RandomAccessFile, causing much confusion with its inconsistent naming.)

提交回复
热议问题