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?
If you write to a file without closing, the data won't make it to the target file.
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've learned why it's important to close files in Python. Because files are limited resources managed by the operating system, making sure files are closed after use will protect against hard-to-debug issues like running out of file handles or experiencing corrupted data.
As long as your program is running, if you keep opening files without closing them, the most likely result is that you will run out of file descriptors/handles available for your process, and attempting to open more files will fail eventually.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With