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?

- 11,553
- 8
- 64
- 88

- 12,368
- 23
- 64
- 105
-
3If you look into Java API, you will be able to get the answer immediately. – gigadot Jan 20 '11 at 20:37
-
51I've learned that people are more helpful than the otherwise superb Java spec. – Albus Dumbledore Jan 20 '11 at 20:40
-
6b/c it cannot be opened :) – bestsss Jan 20 '11 at 20:45
-
6Because it doesn't open anything. And people are considerably *less* reliable than the offical Java specification. – user207421 Dec 18 '14 at 10:08
6 Answers
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.)

- 48,926
- 12
- 77
- 104
java.io.File
doesn't represent an open file, it represents a path in the filesystem. Therefore having close
method on it doesn't make sense.
Actually, this class was misnamed by the library authors, it should be called something like Path
.

- 239,438
- 41
- 511
- 482
-
But then what about all the directory listing methods? They should've been separated from the `Path` completely. – biziclop Jan 20 '11 at 20:40
-
3Agreed, but in any case, either File or RandomAccessFile is wrongly named. Something like RandomAccessFileStream could be better, but it's many year too late now. – maaartinus Jan 20 '11 at 20:48
-
7"Actually, this class was misnamed by the library authors, it should be called something like Path" --> They heard you. In the nio (New I/O) package, the similar classe is now named Path. http://docs.oracle.com/javase/7/docs/api/java/nio/file/Path.html – mins May 25 '14 at 11:42
A BufferedReader can be opened and closed but a File is never opened, it just represents a path in the filesystem.

- 9,015
- 32
- 84
- 152
Essentially random access file wraps input and output streams in order to manage the random access. You don't open and close a file, you open and close streams to a file.

- 2,259
- 1
- 20
- 29
-
1Yes. I never looked much into java.io.RandomAccessFile. I always assumed it overrode java.io.File but it does not!. File represents a path. RandomAccessFile is an object that can perform disk I/O as streams can, albeit with a much different implementation, to allow random rather than streamed access. – Steve Cohen Sep 06 '16 at 15:25
Say suppose, you have
File f = new File("SomeFile");
f.length();
You need not close the File
s, because its just the representation of a path.
You should always consider to close only reader/writers and in fact streams.

- 20,879
- 9
- 40
- 61

- 7,731
- 6
- 43
- 46
As already stated, the File class does not have a closing method as it's merely a path or a reference to the actual File.
You will usually use this File class as a helper to open the actual file with a FileReader class which you can close. That said, it does close itself on exit but if you read a file from your program and then try to do something to this file externally, it could result in an error on that external call, so it's better to close it
File path = new File(/some/path/file.txt);
FileReader actualFile = new FileReader(path);
...<
if(imDoneWithTheFile)
actualFile.close();

- 3
- 3
-
This will fail to close your FileReader if there is an exception somewhere in your hidden block of code. You should always use try-with-resources to close an object implementing the Closeable/AutoCloseable interface, or if you are for some reason unable to (a very old Java version perhaps) do so in a finally-block. – MrThaler Jan 12 '23 at 10:06