简体   繁体   English

如何在Java中保存和加载数组

[英]How to save and load a Array in Java

So I am creating a program which will keep track of the TV shows and Movies that I watch. 所以我正在创建一个程序,用于跟踪我观看的电视节目和电影。 I have 2 classes (TV and Movie) and I have an Array which holds everyone. 我有2个课程(电视和电影),我有一个可以容纳所有人的数组。 How would i go about saving this array so that every time I use the program it lets me edit the same list, because every time I watch a new episode I want to update my array with the new information. 我将如何保存这个数组,以便每次我使用该程序时都可以编辑相同的列表,因为每次观看新剧集时我都想用新信息更新我的数组。 So basically what procedure would I need to use in order to not only save an arrya but load up the array every time I run the program? 那么基本上我需要使用什么程序才能不仅保存arrya而且每次运行程序时加载数组?

In order to save an array for further reference you need to make the class serializable (for example, by implementing Serializable ). 为了保存数组以供进一步参考,您需要使类可序列化(例如,通过实现Serializable )。 What this means is that an object can be converted to a sequence of bytes which could be saved to a file or sent over the network, which can later be used to recreate the object in memory 这意味着一个对象可以转换为一个字节序列,可以保存到文件或通过网络发送,以后可以用来重新创建内存中的对象

Then you can do this to save the data to a file: 然后,您可以执行此操作以将数据保存到文件:

ObjectOutputStream out = new ObjectOutputStream(
    new FileOutputStream("myarray.ser")
);
out.writeObject(myArray);
out.flush();
out.close();

You can read it back like this: 你可以这样读回:

ObjectInputStream in = new ObjectInputStream(new FileInputStream("myarray.ser"));
MyType[] array = (MyType[]) in.readObject();
in.close();

While you could serialize your object and write it to disk... I am guessing it would be easier for you to simply output your array to a file and read it back in. You can write each element in the array to the file in a loop fairly easily, and read it back in just as easily. 虽然您可以序列化您的对象并将其写入磁盘...我猜您将更容易将数组输出到文件并将其重新读入。您可以将数组中的每个元素写入文件中相当容易地循环,并轻松地读回来。 And like the others said, it is worth your time to look into an ArrayList or similar structure! 和其他人说的一样,值得花些时间研究一下ArrayList或类似的结构! Example: 例:

To write to a file: 要写入文件:

ArrayList<String> list = new ArrayList<String>();

// add stuff the the ArrayList

PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("data.txt")));

for( int x = 0; x < list.size(); x++)
{
out.println(list.get(x));
}

out.close()

To read from a file: 要从文件中读取:

ArrayList<String> list = new ArrayList<String>();

Scanner scan = new Scanner(new File("data.txt"));

while(scan.hasNext())
{
list.add(scan.next());
}

Firstly, you should avoid arrays like the plague... use Collections instead, which apart from anything else expand in size automatically as needed (you'll need this). 首先,你应该避免像瘟疫一样的数组...使用Collections代替,除了其他任何东西,根据需要自动扩展大小(你需要这个)。

Regardless of whether you use array or a List you can serialize the object to disk by writing the object to an ObjectOutputStream wrapping a FileOutputStream . 无论您使用数组还是List都可以通过将对象写入包装FileOutputStreamObjectOutputStream来将对象序列化到磁盘。

Note that the objects in the array/List must implement Serializable , as must all your class fields. 需要注意的是,数组的对象/列表必须实现Serializable ,因为必须在所有类字段。

To read the array/List back in, read from an ObjectInputStream wrapping a FileInputStream . 要重新读取数组/列表,请从包装FileInputStreamObjectInputStream读取。

You can Google for the countless examples of such code. 您可以通过Google获取此类代码的无数示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM