1

I am currently writing an application which has a result a file this is composed by blocks of bytes that are processed in blocks so the goal is to process one block convert it into bytes and write(append) that block of bytes into the file then process the next block and so on..until it finishes having all of the bytes of all of the blocks stored into the file, i have been trying using the following piece of code:

try (ObjectOutputStream oos = new ObjectOutputStream(file)) {
    oos.writeObject(bytestobwritten);
    oos.flush();
    oos.close();
    stat = 1;
} catch(FileNotFoundException ex) {
    Logger.getLogger(Filer.class.getName()).log(Level.WARNING, "Error by writing block of bytes", ex);                 
} //end catch 

The above code is inside the while structure that process the bytes the variable bytestobwritten contains the bytes of the current block

The issue here is that it is not appending all of the bytes only remains the last block of bytes..i need all of them being "Concatenated" to make the result amount of bytes for that file..

do you have any idea on how to deal with this situation in java? will appreciate any help, thanks in advance.

4
  • 2
    Where's the loop part of the code? Likely you are looping only while chunks of size n remain. So you need to handle the remaining bytes separately, outside the loop.
    – mellamokb
    Mar 26, 2014 at 23:44
  • 2
    Why are you using an ObjectOutputStream? From your description it seems much more likely that you should just be using a FileOutputStream directly.
    – user207421
    Mar 26, 2014 at 23:51
  • Ok it was because just got it from example will try with FileOutPutStream!! yes right now.. Mar 26, 2014 at 23:56
  • 2
    And then, if you're calling this code in a loop, put the loop inside the try block so you only create the file once. At present you're overwriting the file every time you call this code, so of course you only end up with the result of the last call. You could use the code as-is by opening the FileOutputStream in append mode, but it's rather inefficient that way.
    – user207421
    Mar 27, 2014 at 0:02

1 Answer 1

1

So I'm not sure you understand your problem you're trying to solve. But first ditch ObjectOutputStream for now. We can use OutputStream (or DataOutputStream) to write bytes.

When you talk about writing blocks of bytes to a file you have to answer the question: Are all blocks the same size? That's really important because if you write blocks with varying lengths you won't be able to read it back in because you don't know where a block begins and ends. You will need to know the size of the next block before you read it. If it's fixed block size that changes the code, but fixed block sizes has the limitation that no one block can be bigger than the block size.

public saveBLocks( List<Block> blocks ) {
    DataOutputStream stream = new DataOutputStream( new FileOutputStream( "someFile.txt" ) );
    try {
        for( int i = 0; i < blocks.size(); i++ ) {
           byte[] buffer = createBuffer( blocks.get(i) );

           // save out the block size to the stream if we have varying block size
           stream.writeInt( buffer.length );

           // save the block, assumes buffer is the exact size of the block
           stream.write( buffer, 0, buffer.length );
        }
        stream.flush();
    } finally {
        stream.close();
    }
}

After reading part of your question I wonder if you are just copying bytes between two streams which makes this simpler, and you don't really have to worry about blocks per se.

2
  • This code has been very useful to me, also following the advices mentioned in the others answers, really appreciate this support. Mar 28, 2014 at 2:38
  • maybe Apache Commons IO has some simple but useful methods I guess.
    – Logan Guo
    Apr 11, 2016 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.