Java:BigInteger,如何通过 OutputStream 编写它
我想将 BigInteger 写入文件。
执行此操作的最佳方法是什么。
当然,我想从输入流读取(通过程序,而不是人类)它。
我是否必须使用 ObjectOutputStream 还是有更好的方法?
目的是使用尽可能少的字节。
谢谢
马丁
I want to write a BigInteger to a file.
What is the best way to do this.
Of course I want to read (with the program, not by human) it from an inputstream.
Do I have to use an ObjectOutputStream or are there better ways?
The purpose is to use as less bytes as possible.
Thanks
Martijn
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Java 序列化(
ObjectOutputStream
/ObjectInputStream
)是将对象序列化为八位字节序列的通用方法。但是,序列化存在问题。为了提高效率,
BigInteger
具有toByteArray
和一个采用byte[]
的构造函数。然后您需要某种方法来表示流中的字节[](包括长度)。例如,您可以使用DataOutputStream
来writeInt
长度,然后使用原始数据。当然,可以使用您选择的合适的装饰器来压缩流。
Java serialisation (
ObjectOutputStream
/ObjectInputStream
) is a general purpose way of, er, serialising objects into octet sequences. However, there are issue with serialisation.To be uber efficient,
BigInteger
hastoByteArray
and a constructor that takesbyte[]
. Then you need some way to representbyte[]
(including length) in a stream. For instance, you could useDataOutputStream
towriteInt
the length, and follow that with the raw data.Streams can, of course, be compressed with a suitable decorator of your choice.
我会选择 ObjectOutputStream,这就是它的设计目的(不是专门针对 BigInteger,而是类)。
下面是一些快速示例代码,显示了压缩和未压缩的 ObjectOutpuStream 的开销。
输出是:
您将更改“values.add(BigInteger.ZERO);”线以使测试更加真实 - 我只是想要一个基线。
I'd go with ObjectOutputStream, that is what it was designed for (not BigInteger specifically, but classes).
Here is some quick sample code that shows the overhead for both compresssed and uncompressed ObjectOutpuStreams.
The output is:
You would change the " values.add(BigInteger.ZERO);" line to make the test more realistic - I just wanted a baseline for it.
编辑:我没有意识到这个问题是关于优化的。
您可以随后压缩序列化对象以节省一些字节。尝试使用以下内容。
这是 Sun 的一篇关于它的文章。
Edited: I didn't realize the question was a about optimization.
You could compress the serialized object afterwards to save some bytes. Try using the following.
Here is an article by sun about it.
是的,为了简单起见,您可以使用 ObjectOutputStream/ObjectInputStream,或者您可以将 BigInteger 转换为 byte[],并序列化该值而不是整个 Object。与序列化整个对象相比,后者可以节省大量存储空间。
另外,如果您使用尚未缓冲的流类,请记住将 OutputStreams 和 InputStreams 包装在 BufferedOutputStream 和 BufferedInputStream 中以提高性能,并在完成写入后刷新()(如果您不刷新() BufferedOutputStream,输入流可能会停止或挂起等待输入)。
如果您担心带宽或文件大小,您还可以将流包装在 GZipOutputStream/GZipInputStream 中以进行自动压缩。不过,除非您确实观察到性能不佳或文件巨大,否则我不会担心压缩数据。
Yes, you can use ObjectOutputStream/ObjectInputStream for simplicity, or you can convert the BigInteger to a byte[], and serialize that value instead of the entire Object. The latter would save a significant amount of storage space over serializing the entire Object.
Also, if you use stream classes that are not already buffered, remember to wrap your OutputStreams and InputStreams in BufferedOutputStream and BufferedInputStream to improve performance, and flush() after you're done writing (if you don't flush() the BufferedOutputStream, the InputStream may stall or hang waiting for input).
If you're worried about bandwidth or file size, you can also wrap your streams in GZipOutputStream/GZipInputStream for automatic compression. I wouldn't worry about compressing the data unless you actually observe poor performance or huge files, however.
您想读取/写入整个
对象
还是仅读取/写入其值?如果是前者,则使用序列化。如果是后者,则只需使用ByteArrayInputStream
/ByteArrayOutputStream
其中写入BigInteger#toByteArray()
并在new BigInteger(byte[])
分别。最后一种方法显然在文件中生成的字节少得多。Do you want to read/write the whole
Object
or only its value? If the former, then make use of Serialization. If the latter, then just make use ofByteArrayInputStream
/ByteArrayOutputStream
wherein you write the outcome ofBigInteger#toByteArray()
and construct a new one with help ofnew BigInteger(byte[])
respectively. The last way obviously generates much less bytes in the file.