• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Maintaining File Size while writing

 
Amirr Rafique
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

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

Please help
 
Amirr Rafique
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Any idea!!!

 
Greg Charles
Sheriff
Posts: 3002
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Amirr Rafique
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Grep
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic