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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:23:16+00:00 2026-06-15T07:23:16+00:00

I need to read multiple small files and append them into a bigger single

  • 0

I need to read multiple small files and append them into a bigger single file.

Base64OutputStream baos = new Base64OutputStream(new FileOutputStream(outputFile, true));

for (String fileLocation : fileLocations) {
InputStream fis = null; 
        try
        {
            fis = new FileInputStream(new File(fileLocation));
            int bytesRead = 0;
            byte[] buf = new byte[65536];
            while ((bytesRead=fis.read(buf)) != -1) {
                if (bytesRead > 0) baos.write(buf, 0, bytesRead);
            }
        }
        catch (Exception e) { 
            logger.error(e.getMessage());
        }
        finally{
            try{
                if(fis != null)
                    fis.close();
            }
            catch(Exception e){
                logger.error(e.getMessage());
            }
        }
}

All pretty standard, but I’m finding that, unless I open a new baos per input file (include it inside the loop), all the files following the first one written by baos are wrong (incorrect output).

The questions:

  • I’ve been told that opening/closing an outputstream back and forth for the same resource is not a good practice, why?
  • Why using a single output stream is not delivering the same result as multiple separate ones?
  • 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-15T07:23:18+00:00Added an answer on June 15, 2026 at 7:23 am

    Perhaps the problem is that if you are assumming that encoding in base64 the concatenation of several files should give the same result as concatenating the base64 encoding of each file? That’s not necessariy the case; base64 encodes groups of three consecutive input bytes to 4 ascii characters, so, unless you know that each file has a size that is a multiple of three, the base64 encoding will produce completely different outputs.

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

Sidebar

Related Questions

I have multiple files of text that I need to read and combine into
I need to read test.TXT file(tab delimited) into MATLAB. TXT file have form: Datum
How can I read multiple files as a single ByteString lazily with constant memory?
I would like to do the following: read multiple ascii files (*.txt) into memory.
I have multiple Bluetooth Sockets, and i need to read from each of them
my project need to read multiple web pages at a time(eg: for a particular
I need to read 16 bits from the binary file as std::string or char
I need to read a file line by line, and change a variable accordingly.
I need to read data from XML to a List<>. The XML file contains
I need to read the size of a file but the server is forcing

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.