I need to write save/load logic for my game but I’m new to Java and never used binary i/o. Since I haven’t started writing yet, is there any library that makes it easier or faster(as in writing/reading speed)? Or any good approach to save the data below?
The save data consists of about 2000 units of ( Array-Int(2000),Array-String(100),Array-Boolean(500), Array-Float(500) ).
I know it’s very huge. I used to use xstream to serialize it and the file size will probably become like 100-200mb in raw xml data and also slow to save.
With I/O, the storage device is usally the limiting factor, making the time proportional to file size. Using a binary format instead of XML is certainly a step in the right direction, as it will reduce both filesize and processing overhead.
However, it might be worth looking into reducing size further. For instance, do you really need ints or do shorts suffice? Will the strings use the entire unicode alphabet or can you get away with using a single byte character encoding?
The easiest way to write a binary data file is to use Java Serialization, so I’d try that first (Be sure to use a BufferedOutputStream, of course). Using a simple DataOutputStream will be a little faster in execution time and result in slightly shorter files (due to the absence of type descriptors), but you’ll have to write the fields yourself.
Once you have done that, it worth measuring whether CPU or I/O is the limiting factor (for instance by watching CPU usage as the file is written). If you have spare CPU, and quite regular data, you might speed up writing by compressing the file on the fly, for instance using a GZipOutputStream.