Skip to content
Advertisement

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

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.)

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement