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

  • Home
  • SEARCH
  • 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 52907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:58:49+00:00 2026-05-10T16:58:49+00:00

I just played with Java file system API, and came down with the following

  • 0

I just played with Java file system API, and came down with the following function, used to copy binary files. The original source came from the Web, but I added try/catch/finally clauses to be sure that, should something wrong happen, the Buffer Streams would be closed (and thus, my OS ressources freed) before quiting the function.

I trimmed down the function to show the pattern:

public static void copyFile(FileOutputStream oDStream, FileInputStream oSStream) throw etc... {    BufferedInputStream oSBuffer = new BufferedInputStream(oSStream, 4096);    BufferedOutputStream oDBuffer = new BufferedOutputStream(oDStream, 4096);     try    {        try       {           int c;           while((c = oSBuffer.read()) != -1)  // could throw a IOException          {             oDBuffer.write(c);  // could throw a IOException          }       }       finally       {          oDBuffer.close(); // could throw a IOException       }    }    finally    {       oSBuffer.close(); // could throw a IOException    } } 

As far as I understand it, I cannot put the two close() in the finally clause because the first close() could well throw, and then, the second would not be executed.

I know C# has the Dispose pattern that would have handled this with the using keyword.

I even know better a C++ code would have been something like (using a Java-like API):

void copyFile(FileOutputStream & oDStream, FileInputStream & oSStream) {    BufferedInputStream oSBuffer(oSStream, 4096);    BufferedOutputStream oDBuffer(oDStream, 4096);     int c;     while((c = oSBuffer.read()) != -1)  // could throw a IOException    {       oDBuffer.write(c);  // could throw a IOException    }     // I don't care about resources, as RAII handle them for me } 

I am missing something, or do I really have to produce ugly and bloated code in Java just to handle exceptions in the close() method of a Buffered Stream?

(Please, tell me I’m wrong somewhere…)

EDIT: Is it me, or when updating this page, I saw both the question and all the answers decreased by one point in a couple of minutes? Is someone enjoying himself too much while remaning anonymous?

EDIT 2: McDowell offered a very interesting link I felt I had to mention here: http://illegalargumentexception.blogspot.com/2008/10/java-how-not-to-make-mess-of-stream.html

EDIT 3: Following McDowell’s link, I tumbled upon a proposal for Java 7 of a pattern similar to the C# using pattern: http://tech.puredanger.com/java7/#resourceblock . My problem is explicitly described. Apparently, even with the Java 7 do, the problems remain.

  • 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. 2026-05-10T16:58:50+00:00Added an answer on May 10, 2026 at 4:58 pm

    The try/finally pattern is the correct way to handle streams in most cases for Java 6 and lower.

    Some are advocating silently closing streams. Be careful doing this for these reasons: Java: how not to make a mess of stream handling


    Java 7 introduces try-with-resources:

    /** transcodes text file from one encoding to another */ public static void transcode(File source, Charset srcEncoding,                              File target, Charset tgtEncoding)                                                              throws IOException {     try (InputStream in = new FileInputStream(source);          Reader reader = new InputStreamReader(in, srcEncoding);          OutputStream out = new FileOutputStream(target);          Writer writer = new OutputStreamWriter(out, tgtEncoding)) {         char[] buffer = new char[1024];         int r;         while ((r = reader.read(buffer)) != -1) {             writer.write(buffer, 0, r);         }     } } 

    AutoCloseable types will be automatically closed:

    public class Foo {   public static void main(String[] args) {     class CloseTest implements AutoCloseable {       public void close() {         System.out.println('Close');       }     }     try (CloseTest closeable = new CloseTest()) {}   } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 60k
  • Answers 61k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer If you need to share ressources between web application you… May 11, 2026 at 9:35 am
  • added an answer In your concrete case you should just add Type =… May 11, 2026 at 9:35 am
  • added an answer try this out: var rgbString = 'rgb(0, 70, 255)'; //… May 11, 2026 at 9:35 am

Related Questions

I just played with Java file system API, and came down with the following
I just requested a hotfix from support.microsoft.com and put in my email address, but
I just wonder a bit whether or not GDI+ is still a technology worth
I just wonder what options there are to properly measure/profile/optimize ASP.net 2.0 Web Parts,
I just read up on a performance of LINQ, and there is a HUGE
I just saw this mentioned in Stack Overflow question Best WYSIWYG CSS editor and
I just started thinking about creating/customizing a web crawler today, and know very little
I just saw a comment of suggesting J# , and it made me wonder...
I just moved over to the Visual Basic team here at work. What is
I just started using GNU Emacs as my text editor and I am concerned

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.