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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11661376/
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-10-31 05:57:03  来源:igfitidea点击:

How to save and load a Array in Java

javaarraysloadingsave

提问by werdho

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. 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?

所以我正在创建一个程序来跟踪我观看的电视节目和电影。我有 2 个班级(电视和电影),我有一个容纳所有人的数组。我将如何保存此数组,以便每次使用该程序时它都可以让我编辑相同的列表,因为每次观看新剧集时,我都想用新信息更新我的数组。所以基本上我需要使用什么程序来不仅保存一个 arrya 而且每次运行程序时加载数组?

回答by dasblinkenlight

In order to save an array for further reference you need to make the class serializable (for example, by implementing 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

为了保存数组以供进一步参考,您需要使类可序列化(例如,通过实现Serializable)。这意味着可以将对象转换为字节序列,这些字节可以保存到文件中或通过网络发送,稍后可用于在内存中重新创建对象

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();

回答by dykeag

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! Example:

虽然您可以序列化您的对象并将其写入磁盘......我猜您只需将数组输出到文件并读回它会更容易。您可以将数组中的每个元素写入文件中相当容易地循环,并且同样容易地读回它。就像其他人说的,花时间研究一个 ArrayList 或类似的结构是值得的!例子:

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());
}

回答by Bohemian

Firstly, you should avoid arrays like the plague... use Collectionsinstead, which apart from anything else expand in size automatically as needed (you'll need this).

首先,你应该避免像瘟疫这样的数组......改用Collections它,除了其他任何东西之外,它会根据需要自动扩展大小(你会需要这个)。

Regardless of whether you use array or a Listyou can serialize the object to disk by writing the object to an ObjectOutputStreamwrapping a FileOutputStream.

无论您使用数组还是 a,List您都可以通过将对象写入ObjectOutputStream包装 a来将对象序列化到磁盘FileOutputStream

Note that the objects inthe array/List must implement Serializable, as must all your class fields.

需要注意的是对象数组/列表必须实现Serializable,因为必须在所有类领域。

To read the array/List back in, read from an ObjectInputStreamwrapping a FileInputStream.

要重新读取数组/列表,请从ObjectInputStream包装 a 中读取FileInputStream

You can Google for the countless examples of such code.

您可以在 Google 上搜索无数此类代码的示例。