'How to append 4 bytes(long) header to the file to read the data based on the header position

I want to append 4 bytes long header to a file .So that whenever I write the nextbytes after reopening the file .I can use this 4 bytes header to identify where I left off . This header would act like a position placeholder to identify the no of bytes I want to write .

Following is the output from encrypted file after 3 writes to the same file.And 100,200,300 are header length after each write which I will be maintaining for eg. .I am aware of random access file where I can use seek method to append data at arbitrary position .I have constraints to use this .I can use FIleChannel position but I am not sure how to write bytes starting of each append.

100{Inp2}xxxxxadsasdasdasdasdasdas(end of write1)200{Inp3}xxxxxadsasdasdasdasdasdas(end of write2)500{Inp4}xxxxxadsasdasdasdasdasdasasd(end of write3)

Any help would be appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source