- 如果有专用文件,没有内容,则不读取对象,仅第一次添加列表
- 如果存在具有内容的指定文件(意味着只有一个链表对象),则将其读取,然后将新客户端添加到列表中,并将链表对象写回到该文件中。
您可以通过以下方式执行此操作:
File file = new File("person.txt");boolean toWrite = false;boolean toModify = false;if (file.exists()){ if (file.length() == 0) { toWrite = true; } else { toModify = true; }}if (toWrite || !file.exists()){ FileOutputStream fos = new FileOutputStream(file); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(list); fos.close(); oos.close();}else if (toModify){ FileInputStream fins = new FileInputStream(file); ObjectInputStream oins = new ObjectInputStream(fins); linkedList<Person> temp = (linkedList<Person>)oins.readObject(); fins.close(); oins.close(); temp.add(per); temp.add(per2); temp.add(per3); temp.add(per4); FileOutputStream fos = new FileOutputStream(file); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(temp); fos.close(); oos.close();}


