如何在Java文件中附加arraylist

问题描述 投票:0回答:1

给定的函数写入文件,但是再次调用它会覆盖文件中的先前列表对象,例如

如果数组列表索引0包含名称-“ aman”,则我将此函数称为“ aman”保存在文件中。但是,当名称为“ lavesh”的索引1被调用时,它将覆盖先前的数据

它在文件中,例如“ lavesh”但我想要像“阿曼lavesh“

public void alter_file( ArrayList<Flight_registrie> x) {
    try {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(" Flight Registrie.txt"));
            oos.writeObject(x);
            x.clear();
            oos.close();
    } catch (IOException e) {
        System.out.println("An I/O error occurs");
        e.printStackTrace();
    }   
}

没有错误,但是我的方法可能是错误的

java arraylist append fileoutputstream
1个回答
0
投票

oos.writeObject(x);将写入具有x内容的文件。然后,它将删除旧文件的内容。

您可以通过在true的构造函数后面添加FileOutputStream来避免这种情况。布尔值决定是否要附加到文件。默认是您不想附加数据。

© www.soinside.com 2019 - 2024. All rights reserved.