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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:42:17+00:00 2026-06-11T07:42:17+00:00

With reference to my previous question . I have made the program with the

  • 0

With reference to my previous question.

I have made the program with the following approach:
The program first reads 2k of data from the file and stores it into a byte array.
Then the data to be added to each packet is also stored in an array and both are added to an array list.
The array list is then written to an output stream for the file.

The Code is here:

File bin=chooser.getSelectedFile();
int filesize=(int)bin.length();
int pcount=filesize/2048; 
byte[] file=new byte[filesize];
byte[] meta=new byte[12];
int arraysize=pcount*12+filesize;
byte[] rootfile=new byte[46];
ArrayList al = new ArrayList();
String root;
prbar.setVisible(true);
int mark=0;
String metas;
try{
    FileInputStream fis=new FileInputStream(bin);
    FileOutputStream fos=new FileOutputStream(bin.getName().replace(".bin", ".xyz"));
    ObjectOutputStream os=new ObjectOutputStream(fos);
    root="46kb"+"5678"+"0000"+pcount+"MYBOX"+"13"+"S208";
    rootfile=root.getBytes();
    for(int n=0;n<=pcount;n++)
    {
        fis.read(file, 0, 2048);
        mark=mark+2048;
        int v=(mark/filesize)*100;
        prbar.setValue(v);
        metas="02KB"+"1234"+n;
        meta=metas.getBytes();

        al.add(rootfile);
        al.add(meta);
        al.add(file);
    }
    os.writeObject(al.toArray());
}
catch(Exception ex){
    erlabel.setText(ex.getMessage());
}

The program runs without any errors but the file is not created correctly.
Either the approach is wrong or the code.

Please Help

  • 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-11T07:42:19+00:00Added an answer on June 11, 2026 at 7:42 am

    You appear to be writing your own binary format but you are using ObjectOutputStream which has it own header. writeObject write an Object not data in a manner that lets a Java process deserialize that object e.g. with it class hierarchy and field names.

    For binary, I suggest you use a plain DataOutputStream with a BufferedOutputStream which will be more efficient and do what you want.

    I also suggest you write the data as you generate it rather than using an ArrayList. This will use less memory, make the code simpler and be faster.


    I would write the code more like this

    File bin = chooser.getSelectedFile();
    int filesize = (int) bin.length();
    int pcount = (filesize + 2048 - 1) / 2048;
    byte[] file = new byte[2048];
    
    FileInputStream fis = new FileInputStream(bin);
    String name2 = bin.getName().replace(".bin", ".xyz");
    OutputStream os = new BufferedOutputStream(new FileOutputStream(name2));
    byte[] rootfile = ("46kb" + "5678" + "0000" + pcount + "MYBOX" + "13" + "S208").getBytes("UTF-8");
    
    for (int n = 0; n < pcount; n++) {
        os.write(rootfile);
        byte[] metas = ("02KB" + "1234" + n).getBytes("UTF-8");
        os.write(metas);
    
        int len = fis.read(file);
    
        os.write(file, 0, len);
        int percent = 100 * n / pcount;
        prbar.setValue(percent);
    }
    ow.close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I cut&pasted the below code from a previous question into a file called avishay.cpp
Following on from a previous question, I have the following code: var results =
I have many objects reference the same class of stored data. In previous programs,
Please use my previous question as reference. I have marked an answer already. Question
Following up on my previous newbie question about Paypal , I have a new
This is a continuation question from a previous question I have asked I now
With reference to to an previous question of mine: link I have an additional
In reference with this question We have a header with a date picked component
In relation to this previous question I am trying to create a batch file
This is an elaboration of a previous question, but as I delve deeper into

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.