gpt4 book ai didi

java - 将 ArrayList 保存到文件

转载 作者:行者123 更新时间:2023-12-01 22:19:20 25 4
gpt4 key购买 nike

我有一个 Arraylist,我想将其保存在一个文件中以便下次在应用程序中使用,我已经阅读了几个可以使用此代码来执行此操作的地方,但它不起作用,它返回一个打印出 3 后立即出错:

 private void savegame(){
try {System.out.println("1");
FileOutputStream preout = new FileOutputStream(new File("savedgame.ser"));
System.out.println("2");
ObjectOutputStream out = new ObjectOutputStream(preout);
System.out.println("3");
out.writeObject(kortene);
System.out.println("4");
out.close();
System.out.println("5");
preout.close();
System.out.println("6");
output = new FileWriter(new File("saved game settings.txt"));
System.out.println("7");
output.write(Indstillinger.BilledeMappe+"\n"+Indstillinger.AntalKort+
"\n"+Indstillinger.AntalSpillere+"\n"+clicks+"\n"+Score);
System.out.println("8");
output.close();
System.out.println("game save success");
}catch (Exception e) {
System.out.println("game save failed");
}

“kortene”是一个像这样创建的ArrayList:

public static ArrayList<Kort> kortene = new ArrayList<Kort>();

Kort 是我制作的一个类,但我认为这与这个问题无关..

我得到的错误是:java.io.NotSerializedException: java.awt.image.BufferedImage

但我没有 BufferedImage,我只是在每个类中都有一个普通图像......

最佳答案

相信编译器:如果它说它使用 BufferedImage 将图像保存在内存中,那么这是真的。 javadoc 说它不是可序列化的:

http://download.oracle.com/javase/1.5.0/docs/api/java/awt/image/BufferedImage.html

您可以将图像标记为 transient ,并在稍后反序列化时从文件系统重新初始化它们。尝试一下。

关于java - 将 ArrayList 保存到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5770198/

25 4 0