将多个 Java 对象写入单个文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3470138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:04:18  来源:igfitidea点击:

writing many java objects to a single file

javaserialization

提问by Anupam

how can I write many serializable objects to a single file and then read a few of the objects as and when needed?

如何将许多可序列化对象写入单个文件,然后在需要时读取一些对象?

采纳答案by Andrzej Doyle

You'd have to implement the indexing aspect yourself, but otherwise this could be done. When you serialize an object you essentially get back an OutputStream, which you can point to wherever you want. Storing multiple objects into a file this way would be straightforward.

您必须自己实现索引方面,否则可以这样做。当您序列化一个对象时,您基本上会返回一个OutputStream,您可以将其指向任何您想要的位置。以这种方式将多个对象存储到一个文件中会很简单。

The tough part comes when you want to read "a few" objects back. How are you going to know how to seek to the position in the file that contains the specific object you want? If you're always reading objects back in the same order you wrote them, from the start of the file onwards, this will not be a problem. But if you want to have random access to objects in the "middle" of the stream, you're going to have to come up with some way to determine the byte offset of the specific object you're interested in.

当您想读回“一些”对象时,困难的部分就来了。您将如何知道如何在文件中查找包含您想要的特定对象的位置?如果您总是按照您编写对象的相同顺序读取对象,从文件的开头开始,这将不是问题。但是,如果您想随机访问流“中间”中的对象,则必须想出某种方法来确定您感兴趣的特定对象的字节偏移量。

(This method would have nothing to do with synchronization or even Java per se; you've got to design a scheme that will fit with your requirements and environment.)

(此方法与同步甚至 Java 本身无关;您必须设计一个适合您的要求和环境的方案。)

回答by Andreas Dolk

The writing part is easy. You just have to remember that you have to write all objects 'at once'. You can't create a file with serialized objects, close it and open it again to append more objects. If you try it, you'll get error messages on reading.

写作部分很简单。您只需要记住您必须“一次”编写所有对象。您无法使用序列化对象创建文件,关闭它并再次打开它以附加更多对象。如果您尝试一下,您会在阅读时收到错误消息。

For deserializing, I think you have to process the complete file and keep the objects you're interested in. The others will be created but collected by the gc on the next occasion.

对于反序列化,我认为您必须处理完整的文件并保留您感兴趣的对象。其他对象将在下一次创建但由 gc 收集。

回答by Chris Lercher

I'd use a Flat File Database(e. g. Berkeley DB Java Edition). Just write your nodes as rows in a table like:

我会使用平面文件数据库(例如Berkeley DB Java 版)。只需将您的节点写为表中的行,例如:

Node
----
id
value
parent_id

回答by Wade

Make Object[]for storing your objects. It worked for me.

Object[]存储你的对象。它对我有用。

回答by WikkoDoc

To read more Objects from file:

要从文件中读取更多对象:



public class ReadObjectFromFile {

    public static Object[] readObject() throws IOException {
        Object[] list = null;

        try {
            byte[] bytes = Files.readAllBytes(Paths.get("src/objectFile.txt"));
            ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
            list = (Object[]) ois.readObject();
            ois.close();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return list;
    }
}