• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why must we close BufferedWriter when finish writing?

 
Kathy E Garret
Greenhorn
Posts: 4
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

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?

 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kathy E Garret
Greenhorn
Posts: 4
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see, thanks Rob.

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
 
Paul Clapham
Sheriff
Posts: 21117
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this code (change the name of the output file if you need to):

After you run it, go and look at the file you thought you wrote "Test Me" to. You'll find it's empty.
 
Greg Charles
Sheriff
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kathy E Garret wrote:Would calling close() make the BufferedWriter immediately become eligible for GC?

No, the same rules apply. But the operating system's file handler will immediately be closed, and that's something you really want. That, and all the stuff Paul and Greg mentioned.
 
Kathy E Garret
Greenhorn
Posts: 4
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ahhh such enlightenment thanks Greg, thanks Paul and thanks again Rob
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic