This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I have been using BufferedWriter for a while now, sometimes I forget to close the BufferedWriter when done writing. Nothing bad actually ever happened, so I was just wondering why should we ever close it?
In your case you probably don't open many files. If you do, and you don't close them, your operating system will be unable to open any more files after a while. Also, while your BufferedWriter has not been garbage collected, the file it was writing to will remain open as far as your operating system is concerned. If that's Windows, you will not be able to delete the file until the file handle is closed, either by the BufferedWriter finally being garbage collected or by the JVM exiting.
So yeah, always close your resources (not just files) when you're done with them.
About garbage collector though, so if I don't close the BufferedWriter, normal GC rules are still applied right? Such as when no reference to a BufferedWriter remain, it'll become eligible for GC. Would calling close() make the BufferedWriter immediately become eligible for GC?
Apart from file handling problems, what about performance of the machine the JVM is running on? My understanding about BufferedWriter is that it's sort of allocating a section of memory to buffer the char before writing to the connection stream. Which I believe, basically mean that if I have many BufferedWriter open with data inside, the machine would be slowed down. But then if I call flush() often, and not closing the BufferedWriter, there won't be any impact at all on performance right? Eventually, GC will free up the memory and closing or not closing the BufferedWriter won't matter at all.
Sorry about being a bit impractical here, I know we should always close it as a good practice, it's just that I'm can't stand having it unconfirmed
A need to know how things work internally is a good characteristic for a software engineer!
Some things to consider:
- BufferedWriter.close() flushes the buffer to the underlying stream, so if you forget to flush() and don't close, your file may not have all the text you wrote to it.
- BufferedWriter.close() also closes the wrapped Writer. When that's a FileWriter, this will ultimately close a FileOutputStream and tell the OS that you're done writing to the file.
- The garbage collector will automatically call close(), not on the BufferedWriter or the wrapped FileWriter, but on the FileOuputStream. So the OS will be happy, but you have to wait for the GC.
- However, you always want to release OS resources as soon as you no longer need them. This goes for open files, database connections, print queues ... anything. Trust me on this one.
- BufferedWriter.close() does clear up the internal character buffer, so that memory will be available for garbage collection, even while the BufferedWriter itself remains in scope.
If you really want a peek under the covers, most of the Java API's source is available. BufferedWriter is here.