如何将新数据从数组列表写入文件底部?

How to write new data from array list to bottom of file?

我正在尝试将数组列表中的数据写入 .txt 文件 (stock.txt)。我的程序(直到系统)完美地加载了现有数据(增加初始值的条形图告诉程序文件中有多少个 ID,仍在处理它)但我 运行 遇到了问题。

程序将数据加载到程序中没有问题。当我调用 save() 方法时,它会将任何新数据写入 stock.txt 的顶部并完全删除增量编号,从而导致下一个 运行 出现 运行 时间错误。我必须手动删除新数据,添加增量编号,然后 运行 它才能使程序运行。

这是 save() 方法的代码:

    public void save() throws IOException {
    // IMPLEMENTATION
    PrintWriter outfileStock = new PrintWriter(new FileWriter(SHOP_STOCK_DATA_FILE));
    outfileStock.println(identifier);
    outfileStock.println(name);
    outfileStock.println(cost);
    outfileStock.println(quanity);
    for (Item item : product) {
        outfileStock.println(item.getIdentifier());
        outfileStock.println(item.getName());
        outfileStock.println(item.getCost());
        outfileStock.println(item.getQuanity());
    }


    outfileStock.close();

以及 load() 方法的代码:

    public void load() throws FileNotFoundException {

    //Load data for product (array list for stock)


    product.clear(); //clear arraylist to load data from stock.txt into it.
    Scanner infileStock = new Scanner(new FileReader(SHOP_STOCK_DATA_FILE));

        int num = infileStock.nextInt();
        infileStock.nextLine();
        for (int i = 0; i < num; i++) {
            String id = infileStock.nextLine();
            String n = infileStock.nextLine();
            int c = infileStock.nextInt();
            infileStock.nextLine();
            int q = infileStock.nextInt();

            //infileStock.nextLine(); deleted due to NoElementException error, error still occurring
            //infileStock.next(); throwing same error, same line. pls based Whosebug bless me with answers

            infileStock.nextLine(); 

            //Place data from for loop into 1D array "p" via their identifiers from Item.java
            Item p = new Item(id, n, c, q);

            //Add products from 1D array above to a 2D array "product" (ArrayList) to store them temporarily for the shop to load
            product.add(p);

            //Human check to see where the load(); method runs to if an error occurs
            System.out.println("Stock Item Loaded");
            System.out.println(p);
        }

        //No more stock items to load? AWESOME. Close the file, you weren't born in a barn.
        infileStock.close();

加载程序时的打印结果如下:

Stock Item Loaded
1234, Steak pie, 399, 4
Stock Item Loaded
1235, Steak and kidney pie, 450, 2
Stock Item Loaded
1236, Chicken and mushroom pie, 389, 4
Stock Item Loaded
1237, Testpilot pie, 199, 9

添加新数据后,它抛出错误 "NoSuchElementException" 并且库存文件如下所示(注意顶部没有增量数字,这是导致错误的原因):

Stock Item Loaded
runtimeError pie, 299, 55, 1234
Stock Item Loaded
Steak pie, 399, 4, 1235
Stock Item Loaded
Steak and kidney pie, 450, 2, 1236
Stock Item Loaded
Chicken and mushroom pie, 389, 4, 1237

随着新饼图被添加到 stock.txt 文件的顶部,而不是我想要的底部。

您需要使用带有布尔参数的 FileWriter 构造函数,该参数指示是否附加到文件。

public FileWriter(String fileName, boolean append) throws IOException

Constructs a FileWriter object given a file name with a boolean indicating whether or not to append the data written.