Hey,
Im stuck trying to figure out how to insert one byte between two other bytes that have already been created in an output stream:

OutputStream out = new FileOutputStream("MyFile");
try {
            out.write((byte) 70);
            /*Byte should be inserted between these elements*/
            out.write((byte) 40);
            /* but must be performed at end of try, as it is total file size of all bytes*/

            out.write((byte)out.getCount()); // my method for getting file size works

    }

I just need a way for the last out.write, to write the byte between the already existing 70, and 40 bytes previously written. I tried the extended way with an offset and length but it always throws an error, any suggestions?

Recommended Answers

All 4 Replies

two other bytes that have already been created

Where are the bytes that "have already been created"? If in an array and the array is not full, then you need to move all the elements to the right at the insert point. If the array is full, then you need to create a new array and copy the first part, the new byte and the last part of the old array.
When do you know that there is a byte to be inserted? Hopefully before the old bytes have been written to the output.

Can you write a "place-holder" value, eg 0, where the size needs to go, then you can overwrite that with the final value later (using a random access file).
ps One byte doesn't allow for a very big file!

Or write to a byte array, change it there and then write bytes to disk file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.