torkell: (Default)
Thomas ([personal profile] torkell) wrote2006-03-14 11:00 am
Entry tags:

(no subject)

Today's discovery: Java will not flush file streams for you. If you open a BufferedWriter, write some stuff to it, and then don't call close() on it, then Java will happily throw the remains of the buffer away.

This is probably related to the lack of destructors in Java, and the complete and utter uselessness of Object.finalize() - the method that's like a destructor, except it only gets called if the garbage collector runs, and then only if the garbage collector feels like it.

[identity profile] pteppic.livejournal.com 2006-03-14 01:13 pm (UTC)(link)
Does Java have a wait for pending finalizers like .NET? Could be your friend here...

And by now, surely Java will let you directly call the GC.

[identity profile] pewterfish.livejournal.com 2006-03-14 02:14 pm (UTC)(link)
Since Java 1.2, I believe. System.gc() is your friend, assuming your code is written in such a way that the garbage collector can safely do its thing.

You should be flushing your buffers anyway. Dumping the buffer to stream on close isn't always the right thing to do if the thread of execution terminates unexpectedly.