I have a huge text file, which I have to parse and write relevant records to the binary file. I need to keep the length of each record fixed, so that they can be retrieved easliy.
At present the approach I am following is :
(1) Create a class with long,floats and StringBuffer( set to a fixed length) fields.
(2) I instantiate the above class, fill data and serialize it.
The problem I am facing is that the space occupied by each record is much more than what I can account for by counting the space required by each of the fields ( for ex: a long will need 4 bytes, float 4 bytes. A char 1 or 2 bytes. So an object should occupy almost as much space as the sum of spaces needed by each of its fields)
Question 1 : Has any one faced a similar problem and solved it successfully.
Question 2 : I am thinking of writing individual fields directly to binary file, without incapsulating into any object. My problem is that since some of the data is in form of Strings, different records are occupying different spaces in memory. Is there any way I can make the String data as of constant length ( char[] does not work, StringBuffer.setLength(someConstant).toString().toBytes() does not work)
Question 3 : Also I faced one strange behaviour. When I instantiate an object and serialize it and simply change the value of fields of that object and serialize it , the serialization does not happen properly. But if I create a new object and populate its fields with the values for the second record, it serializes properly. Can any one explain this ?
Thanks
Mohan
At present the approach I am following is :
(1) Create a class with long,floats and StringBuffer( set to a fixed length) fields.
(2) I instantiate the above class, fill data and serialize it.
The problem I am facing is that the space occupied by each record is much more than what I can account for by counting the space required by each of the fields ( for ex: a long will need 4 bytes, float 4 bytes. A char 1 or 2 bytes. So an object should occupy almost as much space as the sum of spaces needed by each of its fields)
Question 1 : Has any one faced a similar problem and solved it successfully.
Question 2 : I am thinking of writing individual fields directly to binary file, without incapsulating into any object. My problem is that since some of the data is in form of Strings, different records are occupying different spaces in memory. Is there any way I can make the String data as of constant length ( char[] does not work, StringBuffer.setLength(someConstant).toString().toBytes() does not work)
Question 3 : Also I faced one strange behaviour. When I instantiate an object and serialize it and simply change the value of fields of that object and serialize it , the serialization does not happen properly. But if I create a new object and populate its fields with the values for the second record, it serializes properly. Can any one explain this ?
Thanks
Mohan