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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:32:18+00:00 2026-05-26T05:32:18+00:00

The following example shows how to copy file using streams. private void copyWithStreams(File aSourceFile,

  • 0

The following example shows how to copy file using streams.

private void copyWithStreams(File aSourceFile, File aTargetFile, boolean aAppend) {
log("Copying files with streams.");
ensureTargetDirectoryExists(aTargetFile.getParentFile());
InputStream inStream = null;
OutputStream outStream = null;
try{
  try {
    byte[] bucket = new byte[32*1024];
    inStream = new BufferedInputStream(new FileInputStream(aSourceFile));
    outStream = new BufferedOutputStream(new FileOutputStream(aTargetFile, aAppend));
    int bytesRead = 0;
    while(bytesRead != -1){
      bytesRead = inStream.read(bucket); //-1, 0, or more
      if(bytesRead > 0){
        outStream.write(bucket, 0, bytesRead);
      }
    }
  }
  finally {
    if (inStream != null) inStream.close();
    if (outStream != null) outStream.close();
  }
}
catch (FileNotFoundException ex){
  log("File not found: " + ex);
}
catch (IOException ex){
  log(ex);
}
}

private void ensureTargetDirectoryExists(File aTargetDir){
if(!aTargetDir.exists()){
  aTargetDir.mkdirs();
}
}
private static void log(Object aThing){
  System.out.println(String.valueOf(aThing));
}

For the above code snippet, I feel confused about four points:

1) Bucket is allocated as byte[] bucket = new byte[32*1024]; Is there any criterion for choosing the size, such as 32*1024

2) Why it has to “catch” here? Is there any rule for including catch in writing program?

3) I am also not very clear about the usage of “try” here. It seems the author uses nested try in this program.

  • 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-26T05:32:19+00:00Added an answer on May 26, 2026 at 5:32 am

    1) There isn’t a criterion to define the size of your byte array. In the given code it’s used 32 kbytes but you could use any value. The size of the byte array and the size of your file determinate how many times you’ll read the file, bigger buffers results in less reading calls, but aren’t necessary if you’re working with small files.

    2) Whenever you use an method in Java that can throws an exception, you need to catch that exception and do something with it. You can treat the exception in your code (usually print the stack trace to debugging purposes) or throw it, which means that when someone use your code will need to catch your code’s exception.

    3) What he did was to catch the two possible exceptions and specifies which one occurs. Since FileNotFoundException extends IOException he could simply use one try, and catch only the IOException, he coded that way to know if the IOException is a FileNotFoundException or any other IOException. Personally, I wouldn’t write the code the same way the author did, it isn’t easily readable.

    Maybe rewriting the code it gets easier to you understand the tries and catchs:

            System.out.println("Copying files with streams.");
        InputStream inStream = null;
        OutputStream outStream = null;
        try {
            inStream = new BufferedInputStream(new FileInputStream(aSourceFile));
            outStream = new BufferedOutputStream(new FileOutputStream(aTargetFile, aAppend));
        } catch (FileNotFoundException ex){
            // TODO Handle FileNotFoundException
            ex.printStackTrace();
        }
        try {
            byte[] bucket = new byte[32*1024];
            int bytesRead = 0;
            while(bytesRead != -1){
              bytesRead = inStream.read(bucket); //-1, 0, or more
              outStream.write(bucket, 0, bytesRead);
            }
        } catch (IOException ex){
            // TODO Handle IOException
            ex.printStackTrace();
        } finally {
            try {
                if (inStream != null) inStream.close();
                if (outStream != null) outStream.close();
            } catch (IOException ex){
                // TODO Handle IOException
                ex.printStackTrace();
            }
        }
    

    It does the same stuff.

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

Sidebar

Related Questions

The following small example shows my problem: template<class T> struct X { static void
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
I was trying the following example, but with external URLs: Using WebViews The example
I'm able to copy the following .htm file to my sony ericsson C510 and
The following example shows what I want to do: >>> test rec.array([(0, 0, 0),
The following doesn't compile, how can I do this? I think the example shows
I have a view that supports copy and shows the edit menu using the
The following example shows a simple Marker (standard icon) on Google maps for the
I'm following this example: http://www.codinghorror.com/blog/2005/12/getting-started-with-indexing-service.html However, the conversion to dataset shows empty columns for
Can someone please derive a concrete example from the following: http://www.urdalen.com/blog/?p=210 ..that shows how

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.