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.
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.
A BufferedReader can be opened and closed but a File is never opened, it just represents a path in the filesystem.
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
.
Source: Stackoverflow.com