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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:01:33+00:00 2026-06-13T07:01:33+00:00

I have this program that reads 2 Kb Data from a binary file adds

  • 0

I have this program that reads 2 Kb Data from a binary file adds some header to it and then writes it to a new file.
The code is

try {
        FileInputStream fis = new FileInputStream(bin);
        FileOutputStream fos = new FileOutputStream(bin.getName().replace(".bin",    ".xyz"));
        DataOutputStream dos=new DataOutputStream(fos);  
        fos.write(big, 0, big.length);
        for (int n = 1; n <= pcount; n++) {          
            fis.read(file, mark, 2048);
            mark = mark + 2048;             
            prbar.setValue(n);
            prbar.setString("Converted packets:" + String.valueOf(n));
            metas = "2048";
            meta = metas.getBytes();
            pc = String.valueOf(file.length).getBytes();
            nval = String.valueOf(n).getBytes();
            System.arraycopy(pc, 0, bmeta, 0, pc.length);
            System.arraycopy(meta, 0, bmeta, 4, meta.length);
            System.arraycopy(nval, 0, bmeta, 8, nval.length);          
            fos.write(bmeta, 0, bmeta.length);
            fos.flush();
            fos.write(file, 0, 2048);
            fos.flush();
       }
  }catch (Exception ex) {
        erlabel.setText(ex.getMessage());
  }

First it should write the header and then the file.But the output file is full of data that does not belong to the file.It is writing some garbage data.What may be the problem?

  • 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-13T07:01:34+00:00Added an answer on June 13, 2026 at 7:01 am

    It’s not quite clear with some of the declarations missing, but it looks like your problem is with the fis.read() method: the second argument is an offset in the byte array, not the file (common mistake).

    You probably want to use relative reads. You also need to check the return value from .read() to see how many bytes were actually read, before writing the buffer out.

    The common idiom is:

    InputStream is = ... 
    OutputStream os = ...
    
    byte[] buf = new byte[2048];
    int len;
    while((len = is.read(buf)) != -1)
      os.write(buf, 0, len);
    
    is.close();
    os.close();
    

    Edit

    That’s a pretty weird way of writing out your metadata, I assume that’s what the (unused) DataOutputStream is for?

    You don’t need to keep flushing the output stream, just close it when you’re done.

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

Sidebar

Related Questions

I have a program that reads arbitrary data from a file system and outputs
I have a C++ program that will read in data from a binary file
I have this problem: I have a datagridview that reads the data from a
Long story short, I have a Java process that reads and writes data to/from
I have a Web server that reads and writes to a data file on
I have a little gui program that on startup reads data from an Excel
I have a program that reads data from a .csv and creates a multimap
We have an asp.net c# program that reads a sheet from an Excel file
I have a vb program that reads data from an excel sheet and displays
I am writing a FORTRAN program that reads data from a text file and

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.