What is the most efficient way to output a boolean array to (and input from) a file in Java? I was going to use a string with each character being either ‘t’ or ‘f’ and then I thought, why not take eight time less space?
NOTE
I actually have no idea which answer is the better method, I’ve just chosen Peter’s because I understand it. Thanks to both answerers!
Say you have a boolean[]
and you want to write this to a disk, and you don’t care how its is implemented in memory.
prints
but if I look at how big the file actually is
It says the length is 2 bytes, but the disk space used is actually 4 KB.
Note: About 1 minute of your time is worth about the same as 80 MB of SSD (expensive disk, more for HDD) So if you don’t think you will be saving at least 80 MB by using this, you could be wasting your time. 😉
You can use BitSet, which can take 16x less space as each character is 16-bit.