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?
Advertisement
Answer
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.)