aspose file tools*
The moose likes I/O and Streams and the fly likes Why must we close BufferedWriter when finish writing? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Why must we close BufferedWriter when finish writing?" Watch "Why must we close BufferedWriter when finish writing?" New topic
Author

Why must we close BufferedWriter when finish writing?

Kathy E Garret
Greenhorn

Joined: Jul 12, 2011
Posts: 4

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?


SCJP6
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Kathy E Garret
Greenhorn

Joined: Jul 12, 2011
Posts: 4

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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Oct 01, 2001
Posts: 2851
    
  11

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

Joined: Oct 27, 2005
Posts: 19693
    
  20

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

Joined: Jul 12, 2011
Posts: 4

ahhh such enlightenment thanks Greg, thanks Paul and thanks again Rob
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why must we close BufferedWriter when finish writing?