Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8952741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:56:29+00:00 2026-06-15T13:56:29+00:00

I’m currently doing something like this in java to free resources. Do I need

  • 0

I’m currently doing something like this in java to free resources. Do I need to call close on the BufferedWriter object or is it called in the destructor? With regard to Resource1 and Resource2, I must call release and free respectfully. Is this the correct approach with nested trys? Python has a really nice “with statement” which associates resources with a code scope. Does java have the likes?

Resource1 r1 = new Resource1();
try
{
    ...
    Resource2 r1 = new Resource2();
    try
    {
        ...
        java.io.BufferedWriter f = new java.io.BufferedWriter(new java.io.FileWriter(new java.io.File("f")));
        try
        {
           ...
        }
        finally
        {
           f.close(); 
        }
    }
    finally
    {
        r2.release(); 
    }
}
finally
{
    r1.free(); 
}

UPDATE:
I’m using Java 1.6

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:56:31+00:00Added an answer on June 15, 2026 at 1:56 pm

    What you have with respect to BufferedWriter is correct.

    However, you may want to swallow any exceptions that occur during close. This is annoying but necessary. Often the following pattern is used:

    BufferedWriter f = null;
    try {
      f = new BufferedWriter(...);
      ...
    }
    finally {
      try {
        if(f != null) {
          f.close();
        }
      }
      catch(IOException e) {
        // Nothing can be done, except maybe log it.
      }
    }
    

    IOUtils.finallyClose() can be used to remove the need for the catch on close.

    Java doesn’t quite have the concept of destructors, but it does have finalize() that get’s call when an object is garbage collected. You should never depend on finialize() to close or otherwise cleanup an object.

    Btw, Java 7 introduced a shorter syntax for closing objects that implement Closeable.

    try (BufferedWriter f = new BufferedWriter(new FileWriter(new File("f"))) {
      ...
    }
    

    EDIT: I expanded on finally..close.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am currently running into a problem where an element is coming back from
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.