gpt4 book ai didi

java - 通过 GZIP 流读写对象?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:51:27 25 4
gpt4 key购买 nike

我是 Java 新手。我想学习使用 GZIPstreams。我已经试过了:

ArrayList<SubImage>myObject = new ArrayList<SubImage>(); // SubImage is a Serializable class

ObjectOutputStream compressedOutput = new ObjectOutputStream(
new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(
new File("....")))));
compressedOutput.writeObject(myObject);

ObjectInputStream compressedInput = new ObjectInputStream(
new BufferedInputStream(new GZIPInputStream(new FileInputStream(
new File("....")))));
myObject=(ArrayList<SubImage>)compressedInput.readObject();

当程序将 myObject 写入文件时没有抛出任何异常,但是当它到达该行时

myObject=(ArrayList<SubImage>)compressedInput.readObject();

它抛出这个异常:

Exception in thread "main" java.io.EOFException: Unexpected end of ZLIB input stream

我该如何解决这个问题?

最佳答案

您必须刷新并关闭您的输出流。否则,至少 BufferedOutputStream 不会将所有内容写入文件(它会大批量写入以避免影响性能)。

如果您调用 compressedOutput.flush()compressedOutput.close() 就足够了。

您可以尝试编写一个简单的字符串对象,并检查文件是否编写良好。

如何?如果您编写了一个 xxx.txt.gz 文件,您可以使用您喜欢的 zip 应用程序打开它并查看 xxx.txt。如果应用程序提示,则内容未写完整。

评论的扩展答案:压缩更多数据

改变序列化

如果它是您自己的对象,您可以更改 SubImage 对象的标准序列化。检查java.io.Serializable javadoc知道怎么做。这非常简单。

只写你需要的

序列化的缺点是需要在您编写的每个实例之前写上“它是一个子图像”。如果您事先知道那里会发生什么,则没有必要。所以你可以尝试更手动地序列化它。

要写你的列表,而不是写一个对象,直接写符合你列表的值。您将只需要一个 DataOutputStream(但 ObjectOutputStream 是一个 DOS,所以您无论如何都可以使用它)。

dos.writeInt(yourList.size()); // tell how many items
for (SubImage si: yourList) {
// write every field, in order (this should be a method called writeSubImage :)
dos.writeInt(...);
dos.writeInt(...);
...
}

// to read the thing just:
int size = dis.readInt();
for (int i=0; i<size; i++) {
// read every field, in the same order (this should be a method called readSubImage :)
dis.readInt(...);
dis.readInt(...);
...
// create the subimage
// add it to the list you are recreating
}

此方法更手动,但如果:

  1. 你知道要写什么
  2. 很多类型都不需要这种序列化

它非常实惠,而且比 Serializable 的对应物更压缩。

请记住,可以使用其他框架来序列化对象或创建字符串消息(用于 xml 的 XStream、用于二进制消息的 Google Protocol Buffers,等等)。该框架可以直接处理二进制文件或写入可以随后写入的字符串。

如果您的应用在这方面需要更多信息,或者只是好奇,也许您应该看看它们。

替代序列化框架

刚刚查看了 SO 并发现了几个解决此问题的问题(和答案):

https://stackoverflow.com/search?q=alternative+serialization+frameworks+java

我发现 XStream 非常简单易用。 JSON 是一种非常易读和简洁的格式(和 Javascript 兼容,这可能是一个加号:)。

我应该去:

Object -> JSON -> OutputStreamWriter(UTF-8) -> GZippedOutputStream -> FileOutputStream

关于java - 通过 GZIP 流读写对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12138526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com