如何序列化ArrayList 2x并且不要过度编写已经存在的ArrayList JAVA

我不得不为我的程序创建一个管理面板来序列化更多对象,它的作用是什么,它将对象(String,String)保存到arraylist中,并且arraylist通过序列化保存到文件中。

当试图将另一个对象添加到arraylist时,程序首先将文件内容反序列化为arraylist,因为如果不这样做,我将如何访问文件中已存在的对象。

但是在添加对象3时会发生类似这样的事情

(安慰)

索引0 [[Word:word1 – 提示:提示1],Word:word2 – 提示:hint2]

怎么解决这个?

我想我应该删除旧的arraylist每次创建一个新的对象,但我不知道该怎么做,有什么我可以做的编辑文件中的旧arraylist而不是删除它。 提前致谢

enter code here class Admin{ ArrayList al = new ArrayList(); public void ifwordsarepresent(){ try { FileInputStream fis = new FileInputStream("D:/serial.txt"); ObjectInputStream ois = new ObjectInputStream(fis); if(fis.available()>0){ try { try { al.add((ArrayList) ois.readObject()); addingwords(); ois.close(); } catch public void addingwords(){ try { try { System.out.print("New Word = "); Scanner scan = new Scanner(System.in); String word = scan.next(); System.out.print("New hint = "); Scanner scan2 = new Scanner(System.in); String hint = scan2.nextLine(); FileOutputStream fos = new FileOutputStream("D:/serial.txt"); ObjectOutputStream oos = new ObjectOutputStream(fos); Word newword = new Word(word , hint); al.add(newword); oos.writeObject(al); oos.flush(); oos.close(); }catch (FileNotFoundException e) { e.printStackTrace(); }}catch(IOException e) { System.out.println("Exception during serialization: " + e); } MovingForward(); }