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 7087815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:43:14+00:00 2026-05-28T07:43:14+00:00

Hi all I understand that if we read bytes from an InputStream and we

  • 0

Hi all I understand that if we read bytes from an InputStream and we have finished reading all the bytes (or we do not intend to read to the end of stream), we must call close() to release system resources associated with the stream.

Now I was wondering if I read bytes and it throws a java.io.IOException, am I still required to call close() to release system resources associated with the stream?

Or is it true that on errors, streams are closed automatically so we do not have to call close() ?

  • 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-05-28T07:43:15+00:00Added an answer on May 28, 2026 at 7:43 am

    The OS itself might close the streams and deallocate resources because the process (namely, the JVM) terminates, but it is not mandated to do so.

    You should always implement a finally block where you close it in cases like these, e.g. like this:

    InputStream is = null;
    
    try {
        is = new FileInputStream(new File("lolwtf"));
        //read stuff here
    } catch (IOException e) {
        System.out.println("omfg, it didn't work");
    } finally {
        is.close();
    }
    

    This isn’t really guaranteed to work if it threw in the first place, but you’ll probably wanna terminate at that point anyway since your data source is probably messed up in some way. You can find out more info about it if you keep the InputStream‘s provider around, like, if I kept a ref to the File object around in my example, I could check whether it exists etc via File‘s interface, but that’s specific to your particular data provider.

    This tactic gets more useful with network sessions that throw, e.g., with Hibernate…

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

Sidebar

Related Questions

I understand that the browser does all the work in processing client side scripts
I understand that requests are served by different threads, but do they all come
I'm lead to understand that the following grants all proveleges to all databases that
While I understand this question is fairly vague since I'm not giving you all
I have read Joel's article The Absolute Minimum Every Software Developer Absolutely, Positively Must
I have created a Server app that receives sound from client, i then broadcast
We have a system that stores (single-digit) millions of images, varying in size from
I'm trying to fully understand all of Haskell's concepts. In what ways are algebraic
I am a .NET dev learning Java. As a result, I understand all the
All - I'm trying to understand the first section of the Map file produced

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.