为什么 'BigInteger' 的大小在保存时与 'bitLength' 报告的不同?
Why size of 'BigInteger' differs when saved from what 'bitLength' reports?
我有一个名为 out
的变量,它是一个 BigInteger
。
当尝试使用
在 位 中获取此变量的长度时
out.bitLength();
我收到 46。
如果我使用
将其保存在文件中
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("./testBig.dat"));
oos.writeObject(out);
oos.close();
我得到一个 208 字节 的文件。
有人可以向我解释为什么这两个值不同吗?
这是因为ObjectOutputStream
stores objects in Java's serialization format;它不仅存储 BigInteger
对象的原始内容。
您只能通过反序列化来读回内容,例如使用 ObjectInputStream
。
我有一个名为 out
的变量,它是一个 BigInteger
。
当尝试使用
out.bitLength();
我收到 46。
如果我使用
将其保存在文件中ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("./testBig.dat"));
oos.writeObject(out);
oos.close();
我得到一个 208 字节 的文件。
有人可以向我解释为什么这两个值不同吗?
这是因为ObjectOutputStream
stores objects in Java's serialization format;它不仅存储 BigInteger
对象的原始内容。
您只能通过反序列化来读回内容,例如使用 ObjectInputStream
。