This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I am writing my data contained in POJO to a file. I want to make sure the file does not exceeds to a fixed size e.g. 1MB. Say I have list of 50 Objects of Person type each objects size may vary due to string attributes length. While writing these objects I want to make sure my file size does not exceeds from 1MB. How can I calculate size of object before writing it to stream or how can I undo last written object on stream
"Know where to find the solution and how to use it - that's the secret of success."
We generally counsel patience (PatienceIsAVirtue) here at the Ranch. However, I can understand how you'd get antsy after three days with no replies.
One solution is to stream your objects into memory using a ByteArrayOutputStream. That will let you get your serialized object as a byte array, so you can see how big it is. Then you can stream the byte array out to your file. Another solution would be to record the current size of your file, stream the object to it, then truncate the file back to the original size if it got too big.