Please advise on finalize() to close files

Posted on 2007-10-18
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

    Use a try...finally block
    LVL 86

    Accepted Solution

    PrintWriter out = null;
    try {
    catch(Exception e) {
    finally {
    LVL 92

    Assisted Solution

    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 :)
    LVL 24

    Assisted Solution

    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

    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

    >> 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


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    VB Script 3 60
    wordsCount challenge 10 49
    maven java path setting 5 33
    Which is the latest version of Apache Tomcatl 3 21
    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
    This video teaches viewers about errors in exception handling.

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now