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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:07:32+00:00 2026-06-13T02:07:32+00:00

I have to write byte arrays in a file. I can’t do it in

  • 0

I have to write byte arrays in a file. I can’t do it in one time so I can’t put my arrays in a container. Also the size of my arrays is variable.
Secondly, the file is very huge, so I have to split it, in order to read it array by array.

How can I do that ? I tried to write line by line my byte arrays but I haven’t been able. How can I put a separator between my arrays and after split it over this separator ?

EDIT :

I tried this :

ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(bos);
out.writeObject(byteArray);

But, I execute this code several times, so the ObjectOutputStream adds each time a new header which corrupt the file.

I also try :

out.write(byteArray);

but I couldn’t separate my arrays. So I tried to append a ‘\n’, which didn’t work. After I was looking for library like FileUtils in order to write byte[] line by line but I didn’t find.

  • 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-13T02:07:33+00:00Added an answer on June 13, 2026 at 2:07 am

    You can use existing collections Like e.g. List to maintain List of byte[] and transfer it

        List<byte[]> list = new ArrayList<byte[]>();
        list.add("HI".getBytes());
        list.add("BYE".getBytes());
    
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
                "test.txt"));
        out.writeObject(list);
    
        ObjectInputStream in = new ObjectInputStream(new FileInputStream(
                "test.txt"));
        List<byte[]> byteList = (List<byte[]>) in.readObject();
    
        //if you want to add to list you will need to add to byteList and write it again
        for (byte[] bytes : byteList) {
            System.out.println(new String(bytes));
        }
    

    Output:

       HI
       BYE
    

    Another option is use RandomAccessFile. Which will not force you to read complete file and you can skip the data that you don’t want to read.

         DataOutputStream dataOutStream = new DataOutputStream(
                new FileOutputStream("test1"));
        int numberOfChunks = 2;
        dataOutStream.writeInt(numberOfChunks);// Write number of chunks first
        byte[] firstChunk = "HI".getBytes();
        dataOutStream.writeInt(firstChunk.length);//Write length of array a small custom protocol
        dataOutStream.write(firstChunk);//Write byte array
    
        byte[] secondChunk = "BYE".getBytes();
        dataOutStream.writeInt(secondChunk.length);//Write length of array
        dataOutStream.write(secondChunk);//Write byte array
    
        RandomAccessFile randomAccessFile = new RandomAccessFile("test1", "r");
        int chunksRead = randomAccessFile.readInt();
        for (int i = 0; i < chunksRead; i++) {
            int size = randomAccessFile.readInt();
            if (i == 1)// means we only want to read last chunk
            {
                byte[] bytes = new byte[size];
                randomAccessFile.read(bytes, 0, bytes.length);
                System.out.println(new String(bytes));
            }
            randomAccessFile.seek(4+(i+1)*size+4*(i+1));//From start so 4 int + i* size+ 4* i ie. size of i
        }
    

    Output:

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

Sidebar

Related Questions

I would like to write a byte arrays in a file with Camel. But,
I need to write bytes of an IEnumerable<byte> to a file. I can convert
I have to write a C++ application that reads from the serial port byte
i have to write in a file 4bytes representing an integer in little endian
I have a program that handles byte arrays in Java, and now I would
I have a table in Oracle that stores files as byte arrays in a
I have an byte array of an doc file loaded in memory. I would
I have a byte[] array and want to write it to stdout: Console.Out.Write(arr2str(arr)) .
I have a byte[] array, the contents of which represent a TIFF file (as
I want to write a byte[] data to a file and I found this

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.