简体   繁体   English

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

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

I'm trying to write data to a .txt file (stock.txt) from an array list. 我正在尝试将数据从数组列表写入.txt文件(stock.txt)。 My program (a till system) loads the existing data perfectly (bar incrementing the initial value which tells the program how many IDs are in the file, still working on that) but I've ran into a problem. 我的程序(直到系统)完美地加载了现有数据(增加初始值的杠会告诉程序文件中有多少个ID,仍在处理该ID),但是我遇到了一个问题。

The program loads the data into the program without a problem. 程序将数据毫无问题地加载到程序中。 When I call the save() method, it writes any new data to the top of stock.txt and deletes the increment number entirely, causing runtime errors on the next run. 当我调用save()方法时,它将任何新数据写入stock.txt的顶部,并完全删除增量编号,从而在下次运行时导致运行时错误。 I have to manually delete the new data, add the increment number in and then run it for the program to work. 我必须手动删除新数据,在其中添加增量编号,然后运行它以使程序正常工作。

Here's the code for the save() method: 这是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();

And the code for the load() method: 以及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 stackoverflow 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();

The printed result, when loading the program, shows this: 加载程序时,打印结果显示如下:

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

and after adding in new data, it throws the error "NoSuchElementException" and the stock file looks like this (notice no increment number at the top, which is causing the error): 在添加新数据之后,它将引发错误“ 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

With the new pie being added to the top of the stock.txt file instead of the bottom as I'd like. 将新的饼添加到stock.txt文件的顶部,而不是我想要的底部。

You need to use the FileWriter constructor that takes a boolean argument indicating whether to append to the file or not. 您需要使用带有布尔参数的FileWriter构造函数,该布尔参数指示是否附加到文件。

public FileWriter(String fileName, boolean append) throws IOException public FileWriter(String fileName,boolean append)抛出IOException

Constructs a FileWriter object given a file name with a boolean indicating whether or not to append the data written. 构造一个给定文件名的FileWriter对象,该文件名带有一个布尔值,该布尔值指示是否追加写入的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM