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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:16:52+00:00 2026-06-01T07:16:52+00:00

I am trying to copy a file using the following code: 1: int data=0;

  • 0

I am trying to copy a file using the following code:

1:

        int data=0;
        byte[] buffer = new byte[4096];
        while((data = bufferedInputStream.read())!=-1){
            bufferedOutputStream.write(data);
        }

2:

        byte[] buffer = new byte[4096];
        while(bufferedInputStream.read(buffer)!=-1){
            bufferedOutputStream.write(buffer);
        }

Actual size of file is 3892028 bytes(on windows). The file will be uploaded by the user thro struts2 fileupload. Uploaded file size is exactly same as that of windows. When I try to copy the uploaded file from temporary folder, the copied file varies in size and the time taken also varies(it is negligible). Please find the below readings.

Without using buffer(Code 1)
Time taken 77
3892028
3891200

Buffer size 1024(Code 2)
Time taken 17
3892028
3891200

Buffer size 4096(Code 2)
Time taken 18
3892028
3891200

Buffer size 10240(Code 2)
Time taken 14
3892028
3901440

Buffer size 102400(Code 2)
Time taken 9
3892028
3993600

If I increase the buffer size further, time taken increases, again it is negligible. So my questions are,

  1. Why the file size changes?
  2. Is there any subtle consequences due to this size variation?
  3. What is the best way to accomplish this functionality(copying a file)?

I don’t know what is going beneath? Thanks for any suggestion.
Edit: I have flush() and close() method calls.
Note: I have trimmed my code to make it simpler.

  • 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-01T07:16:53+00:00Added an answer on June 1, 2026 at 7:16 am
    1. Why the file size changes?


    You forgot to `flush()` (and `close()`):

    bufferedOutputStream.flush()

    Also you should pass the number of bytes read to write method:

    bufferedOutputStream.write(data, 0, bytesRead);
    
    1. What is the best way to accomplish this functionality(copying a file)?
    • FileUtils.copyFile()
    • IOUtils.copy()

    Both from apache-commons IO.

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

Sidebar

Related Questions

I'm trying to copy files inside a Python script using the following code: inf,outf
Using the following code, I am trying to overwrite a file if it exists.
I am trying to read a single file from a java.util.zip.ZipInputStream , and copy
I am trying to connect to an mdf file using the following connection string:
I'm trying to convert the following bash code into C++ using boost::iostreams: #!/usr/bin/bash (
I am trying to create a new XML file from an exisiting one using
I am trying to copy a file from my remote server to my local.
I'm trying copy a single file from the Plugin directory inside of my Wordpress
Basically I'm trying to copy a file from one place to another but I
I'm trying to copy both an image from a file and text from 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.