如何在磁盘上保存列表项而不是Java中的内存 [英] How to save list items on disk instead of memory in Java

查看:72
本文介绍了如何在磁盘上保存列表项而不是Java中的内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找与Java中的ArrayList相同的数据结构,它将项目保存在磁盘上而不是内存中。
java有这样的数据结构吗?
谢谢

I'm looking for a data structure same as ArrayList in Java that saves items on disk instead of memory. Does java have such a data structure? Thanks

我希望有一个动态结构可以保存内存中的项目,当它的大小超过某个值时,将新项目保存在磁盘上,直到大小低于值。

I want to have a dynamic structure that saves items on memory and when its size exceeds some value, save new items on disk until the size goes below the value.

推荐答案

你可以自己做:序列化 ArrayList 到磁盘。

You can yourself do it: Serialize the ArrayList to disk.

确保列表的内容是可序列化的,即列表中的对象应实现 Serializable 接口。

Make sure that the contents of the list are serializable, i.e., the objects in the list should implement the Serializable interface.

然后

写入文件:

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileName));
oos.writeObject(list);
oos.flush();
oos.close();

从文件中读取:

ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileName));
List<YourClass> list = ois.readObject();
ois.close()

这篇关于如何在磁盘上保存列表项而不是Java中的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆