如何在硬盘上存储对象?

时间:2022-10-11 03:22:42

I'm working on a project that uses a couple maps that can have over 100,000 keys. Currently I'm creating the maps at each runtime using an abbreviated form of the data to save time so that the maps only have around 1,000 keys. But I would like to test with the full extent of data that I have available.

我正在开发一个项目,该项目使用了几张可以拥有100,000多个密钥的地图。目前我正在使用缩写形式的数据在每个运行时创建地图以节省时间,以便地图只有大约1,000个键。但我想测试我可用的全部数据。

I'm wondering how I can create a map in a class, then save that object to the hard drive, then be able to reference that object in different classes so that I do not need to create and fill the map each time I test the class. I'm writing the class in Java using Eclipse.

我想知道如何在类中创建一个地图,然后将该对象保存到硬盘驱动器,然后能够在不同的类中引用该对象,这样我每次测试时都不需要创建和填充地图类。我正在使用Eclipse在Java中编写类。

Edit: I believe it is called Object Serialization, but would I have to read the whole map before use if it's serialized? Or would it effectively be the same as calling a local variable?

编辑:我相信它被称为对象序列化,但如果它被序列化,我必须在使用前阅读整个地图吗?或者它实际上是否与调用局部变量相同?

3 个解决方案

#1


0  

I had similar problem. First I used HSQLDB. After that I checked EHCache. And it makes the difference - works faster and is easier to understand.

我有类似的问题。首先我使用了HSQLDB。之后我检查了EHCache。而且它有所不同 - 工作得更快,更容易理解。

You also can look on NOSQL page, paragraph "Key Value / Tuple Store". For sure you will find something for you.

您还可以查看NOSQL页面,“Key Value / Tuple Store”段落。当然,你会找到适合自己的东西。

#2


0  

If your map contains objects which are serializable then you can directly write map to the disk using serailization. Eg: Map of String,Integer

如果您的地图包含可序列化的对象,那么您可以使用serailization直接将地图写入磁盘。例如:String,Integer的Map

But if your map holds any custom class that you have created then your class must be made serializable by implementing Serializable interface.

但是,如果您的地图包含您创建的任何自定义类,则必须通过实现Serializable接口使您的类可序列化。

You can explicitly decide the way your class is written to disk using serialization by overriding writeObject(ObjectOutputStream s) for writing and readObject(ObjectInputStream ois) for reading back the contents of the class.

您可以通过重写writeObject(ObjectOutputStream s)进行写入和readObject(ObjectInputStream ois)来读取类的内容,从而使用序列化明确决定类写入磁盘的方式。

These methods i.e. writeObject() and readObject() will be called implicitly when serialization and deserialization takes place. Be careful while implementing these methods as you must you must read the elements back in the same order as writing.

当序列化和反序列化发生时,将隐式调用这些方法,即writeObject()和readObject()。在实现这些方法时要小心,因为您必须以与写入相同的顺序读取元素。

Sample example how to implement these methods : Sample example

示例示例如何实现这些方法:示例示例

#3


0  

I faced a similar issue some time ago. I used Berkeley DB, it fitted perfect. It is a good alternative if you ask me: http://docs.oracle.com/cd/E17277_02/html/GettingStartedGuide/index.html

我前段时间遇到过类似的问题。我使用Berkeley DB,它非常完美。如果你问我,这是一个很好的选择:http://docs.oracle.com/cd/E17277_02/html/GettingStartedGuide/index.html

#1


0  

I had similar problem. First I used HSQLDB. After that I checked EHCache. And it makes the difference - works faster and is easier to understand.

我有类似的问题。首先我使用了HSQLDB。之后我检查了EHCache。而且它有所不同 - 工作得更快,更容易理解。

You also can look on NOSQL page, paragraph "Key Value / Tuple Store". For sure you will find something for you.

您还可以查看NOSQL页面,“Key Value / Tuple Store”段落。当然,你会找到适合自己的东西。

#2


0  

If your map contains objects which are serializable then you can directly write map to the disk using serailization. Eg: Map of String,Integer

如果您的地图包含可序列化的对象,那么您可以使用serailization直接将地图写入磁盘。例如:String,Integer的Map

But if your map holds any custom class that you have created then your class must be made serializable by implementing Serializable interface.

但是,如果您的地图包含您创建的任何自定义类,则必须通过实现Serializable接口使您的类可序列化。

You can explicitly decide the way your class is written to disk using serialization by overriding writeObject(ObjectOutputStream s) for writing and readObject(ObjectInputStream ois) for reading back the contents of the class.

您可以通过重写writeObject(ObjectOutputStream s)进行写入和readObject(ObjectInputStream ois)来读取类的内容,从而使用序列化明确决定类写入磁盘的方式。

These methods i.e. writeObject() and readObject() will be called implicitly when serialization and deserialization takes place. Be careful while implementing these methods as you must you must read the elements back in the same order as writing.

当序列化和反序列化发生时,将隐式调用这些方法,即writeObject()和readObject()。在实现这些方法时要小心,因为您必须以与写入相同的顺序读取元素。

Sample example how to implement these methods : Sample example

示例示例如何实现这些方法:示例示例

#3


0  

I faced a similar issue some time ago. I used Berkeley DB, it fitted perfect. It is a good alternative if you ask me: http://docs.oracle.com/cd/E17277_02/html/GettingStartedGuide/index.html

我前段时间遇到过类似的问题。我使用Berkeley DB,它非常完美。如果你问我,这是一个很好的选择:http://docs.oracle.com/cd/E17277_02/html/GettingStartedGuide/index.html