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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:33:13+00:00 2026-06-01T05:33:13+00:00

I will be performing a lecture on Java for students of Physics, and I

  • 0

I will be performing a lecture on Java for students of Physics, and I would like to know how to properly open a file.

In many my proffesional apps I did somethings like that:

  BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("file")));
  try{
    ....
  }finally {
     bufferedWriter.close();
  }

which is IMHO ok, i.e. reader will allways be closed.

When I was putting that in example for my students I was wondering what will happen if constructor of InputStreamReader will throw an exception — FileInputStream will be open, but it will not be closed by my code (since these objects are created outside try-finally block.

So is this right idiom, and if so then why? If it is not right idiom to open a stream please point me the right one!

Edit: I’m looking for idiom that is both correct and very easy to write and understand, physics students are beginners in programming.

Edit: Silly me I copied wrong example — if instead of Readers I use Writers it get’s more complicated.

  • 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-01T05:33:14+00:00Added an answer on June 1, 2026 at 5:33 am

    Reading with input streams

    Prior to Java 7 this is how you’d do it

    InputStream in = null;
    try {
         in = new FileInputStream("simple.csv");
         BufferedReader buf = new BufferedReader(new InputStreamReader(in));
    } finally {
      if (in != null) {
         try {
             in.close();
         } catch (IOException e) {}
      }
    }
    

    For Java 7 you can use Closeable, something like

    try (BufferedReader buf = new BufferedReader(...)) {}
    

    EDIT: Why didn’t I close buf above? Have a look at the source code for BufferedReader.close()

    public void close() throws IOException {
        synchronized (lock) {
            if (in == null)
                return;
            in.close();
            in = null;
            cb = null;
        }
    }
    

    Writing with output streams

    EDIT 2: The same principle applies to writers. However if you’re really interested in flushing a stream when the IOException occurs, then you must check both the writer and the stream for null and try tro close them respectively. That though, gives a lot of extra code. It could look something like this:

    BufferedWriter buf = null;
    OutputStream out = null;
    try {
        out = new FileOutputStream("file");
        buf = new BufferedWriter(new OutputStreamWriter(out));
    } finally {
      if (buf != null) {
         try { buf.close(); } catch (IOException ex){}
      }
      if (out != null) {
         try { out.close(); } catch (IOException ex){}
      }
    }
    

    It’s not very pretty. You could introduce a helper routine to close your streams or look into either Java 7 or Apache IOUtils

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

Sidebar

Related Questions

I am performing the jquery POST correctly, however I would like to call another
Often WinDbg will enter a state where it is *Busy* performing an operation. Often
We are using nHibernate 1.2 in a system which is not performing good. Will
This is the sort of HTML string I will be performing matches on: <span
When performing a dynamic query, RavenDB will typically create a temp index. Retrieving document
After invoking git merge --no-commit <commit> , performing a commit will result in a
I'm performing I/O to a single file from multiple threads. Access to this shared
When performing some action on another user's repo, you will see the message Not
How can I see how many conflicts will result from a merge without actually
How would I implement a system that will keep 20 applications running on a

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.