简体   繁体   中英

java - saving data for minimal file size - as byte objects instead of text?

my java code generates and logs a large number of data to file as it runs at 10-60fps of up to several days. each time point has the following entries

timestamp (long), x coordinate (int - 0-2000), y coordinate (int - 0-1000), grayscale pixel value (byte), direction (short), length (short), width (short), other value (int)

not surprisingly i am trying to minimize file size of the logged data and was hoping to pick your brain on the best strategy for doing this. i have explored two fundamental approaches ... writing ascii or saving binary objects

obj:

FileOutputStream fos = new FileOutputStream(new File(filePathString));
ObjectOutputStream fw_obj = new ObjectOutputStream(fos);
fw_obj.writeObject(aString);

or

txt:

FileOutputStream fos = new FileOutputStream(new File(filePathString));
Writer fw_txt = new BufferedWriter(new OutputStreamWriter(fos));
fw_txt.write(aString);

the object approach has been less effective than what i had hoped. file sizes are roughly equal for the two. any suggestions on what strategy should be best and opinions for pushing this to the max would be very appreciated. Eternally grateful,

In my opinion the best strategy is to convert the values to bytes and then write them to the file

Long + int + int + byte + short + short +short + int

8 + 4 + 4 + 1 + 2 + 2 + 2 + 4 = 27 bytes per data

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM