Please advise on finalize() to close files

Posted on 2007-10-18
Medium Priority
Last Modified: 2010-03-30
Dear Experts,

I have read that Java developers should use finalize() only for emergency purposes. I'm using PrinterWriter to produce a log/report, but when my class hits an unhandled exception, it dies, and my log/report is empty.  So:

Is there a way to make PrintWriter retain it's output even if it doesn't receive a close() ?  (Other than wrapping everything in a try clause).

Failing that, would it be appropriate to put the close() in finalize() ?

Thanks one more time...
Question by:BrianMc1958
LVL 86

Expert Comment

ID: 20101336
Use a try...finally block
LVL 86

Accepted Solution

CEHJ earned 800 total points
ID: 20101351
PrintWriter out = null;
try {
catch(Exception e) {
finally {
LVL 92

Assisted Solution

objects earned 600 total points
ID: 20101423
It'll get closed when the var is gc'ed. failing that you'll have to use a try/finally block.
though u should be avoiding not handling exceptions :)
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

LVL 24

Assisted Solution

sciuriware earned 600 total points
ID: 20101434
finalize() is indeed NOT guaranteed to ever be called.
Only when you KNOW (but when is that?) that garbage collection will
be forced to reclaim all deleted objects, then finalize() will be called.


Author Comment

ID: 20101437
Well, I know that would work.  I was wondering if there is a way to tell the PrintWriter to save it's data.  I believe BufferedWriter does that by default.

If not, what do you think about the finalize() idea?  That way, if anything goes wrong anywhere, it still gets closed.  But like I say, I don't know if that's an appropriate use of finalize().
LVL 86

Expert Comment

ID: 20101481
>> I believe BufferedWriter does that by default.

No - nothing does actually

>>If not, what do you think about the finalize() idea?

Not good, for the reason s. gave. The way i suggested is the correct one
LVL 86

Expert Comment

ID: 20101768

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month5 days, 4 hours left to enroll

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question