Do I need to close files I perform File.getName() on?
17,270
Solution 1
You never have to close File
s, because it is basically a representation of a path. Only Streams and Readers/Writers. In fact, File
does not even have a close()
method.
Solution 2
Only resources needed to be close.
In java API there is a interface Closeable Interface, those classes implement this interface they need to be close after use.
close() //method is in that interface..
And use of close is
It closes the stream and releases any system resources associated with it.
If the stream is already closed then invoking this method has no effect.
File
is no need to be close
Solution 3
That is correct. Note that there is no File.close()
method.
Related videos on Youtube
Author by
user957183
Updated on June 04, 2022Comments
-
user957183 almost 2 years
I'll be having lot of files in a directory. I'll be just getting the file names using
File.getName()
and log them to a log file. I presume, I don't need to close the file since I'm not doing any read/write operation in it.Is this correct?
-
Wyzard over 12 yearsIf you open a file then you need to close it, but the
File
class doesn't represent an open file, it just represents a path.
-