如何在 Java 中将布尔数组转换为二进制数组,反之亦然?
在 Java 中将布尔数组输出到文件(以及从文件输入)的最有效方法是什么?我打算使用一个字符串,每个字符要么是“t”,要么是“f”,然后我想,为什么不减少八倍的空间呢?
注意
我实际上不知道哪个答案是更好的方法,我只是选择了Peter的,因为我理解它。感谢两位解答者!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您有一个 boolean[]
并且您想将其写入磁盘,并且您不关心它在内存中是如何实现的。
打印,
但如果我看看文件实际有多大,
它说长度是 2 个字节,但使用的磁盘空间实际上是 4 KB。
注意:大约 1 分钟的时间大约相当于 80 MB 的 SSD(昂贵的磁盘,HDD 的费用更高),因此,如果您认为使用此方法不会节省至少 80 MB 的时间,那么您可能会浪费您的时间时间。 ;)
您可以使用 BitSet,它可以减少 16 倍的空间,因为每个字符都是 16 位。
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.
全新打造,只为您而生。我将把
BooleanInputStream
留作练习。请注意,第一位现在是文件中最右边的 (MSB) 位(在示例中删除Byte.SIZE - 1 -
以获取其他字节顺序,无论您喜欢什么)。只需使用例如 DataOutputStream 首先将事物的大小写入文件。 10K 应该是一个整数。请注意,存储 10K 元素的布尔数组在内存方面效率非常低,您当然应该使用
BitSet
(最后,有人需要BitSet
)!Newly created, just for you. I'll leave the
BooleanInputStream
as an excercise. Note that the first bit is now the rightmost (MSB) bit in the file (removeByte.SIZE - 1 -
in the example for other byte order, whatever you prefer). Simply use e.g. DataOutputStream to write the size of the thing to file first. 10K should fit in an integer.Note that storing a boolean array of 10K elements is very inefficient memory wise, you certainly should use
BitSet
for that (finally, somebody who needsBitSet
)!