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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:46:41+00:00 2026-05-27T19:46:41+00:00

My basic Java problem is this: I need to read in a file by

  • 0

My basic Java problem is this: I need to read in a file by chunks, then reverse the order of the chunks, then write that out to a new file. My first (naive) attempt followed this approach:

  1. read a chunk from the file.
  2. reverse the bytes of the chunk
  3. push the bytes one at a time to the front of a results list
  4. repeat for all chunks
  5. write result list to new file.

So this is basically a very stupid and slow way to solve the problem, but generates the correct output that I am looking for. To try to improve the situation, I change to this algorithm:

  1. read a chunk from the file
  2. push that chunk onto the front of a list of arrays
  3. repeat for all chunks
  4. foreach chunk, write to new file

And to my mind, that produces the same output. except it doesn’t and I am quite confused. The first chunk in the result file matches with both methods, but the rest of the file is completely different.

Here is the meat of the Java code I am using:

FileInputStream in;
FileOutputStream out, out2;

Byte[] t = new Byte[0];
LinkedList<Byte> reversed_data = new LinkedList<Byte>();
byte[] data = new byte[bufferSize];
LinkedList<byte[]> revd2 = new LinkedList<byte[]>();

try {
in = new FileInputStream(infile);
out = new FileOutputStream(outfile1);
out2 = new FileOutputStream(outfile2);
} catch (FileNotFoundException e) {
    e.printStackTrace();
    return;
}
while(in.read(data) != -1)
{
    revd2.addFirst(data);
    byte[] revd = reverse(data);
    for (byte b : revd)
    {
        reversed_data.addFirst(b);
    }
}
for (Byte b : reversed_data)
{
    out.write(b);
}
for (byte[] b : revd2)  
{
    out2.write(b);
}

At http://pastie.org/3113665 you can see a complete example program (a long with my debugging attempts). For simplicity I am using a bufferSize that divides evenly the size of the file so all chunks will be the same size, but this won’t hold in the real world. My question is, WHY don’t these two methods generate the same output? It’s driving me crazy because I can’t grok it.

  • 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-27T19:46:41+00:00Added an answer on May 27, 2026 at 7:46 pm

    You’re constantly overwriting the data you’ve read previously.

    while(in.read(data) != -1)
    {
        revd2.addFirst(data);
        // ignore byte-wise stuff
    }
    

    You’re adding the same object repeatedly to the list revd2, so each list node will finally contain a reference to data filled with the result of the last read. I suggest replacing that with revd2.addFirst(data.clone()).

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

Sidebar

Related Questions

In basic Java, if I write code to solve a common problem and want
while(rs.next()){ Object o = rs.getObject(i); System.out.println(o.getClass()); //prints java.lang.Integer } The problem is that I
i have a really basic java program just one .java/.class file (.java uncompiled .class
Here's the basic idea: There is a java window (main) that opens another java
Really basic question I'm sure for some of you Java heads out there. I
I am putting together an Java application that needs to display some basic lab
I am learning Java and have a pretty basic problem. I am indexing some
hello i have a basic client-server system running using java sockets. my problem is,
I have been programming Java for around 15 years. While this is a basic
I need to figure out a way to write the contents of a JSF

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.