java - 如何将数组列表中的新数据写入文件底部?

标签 java arraylist runtime

我正在尝试将数据从数组列表写入 .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 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();

加载程序时打印的结果显示:

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 构造函数,该构造函数采用 boolean 参数来指示是否追加到文件。

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.

关于java - 如何将数组列表中的新数据写入文件底部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29106176/

相关文章:

java - java中的绝对路径直接到桌面,即使在不同的计算机上

java - 在 Eclipse 中生成 setter/getter,格式如下 : get0(), get1(), set0(

android - 如何使用 Firebase 实时数据库在不知道 Android 索引的情况下更新 ArrayList 中的元素?

java - 从列表中删除对象和迭代器问题

java - ArrayList#size() 的奇怪返回

java - ImageView 不显示来自 java android Intent 的图像

java - EJB @Lock 未按预期工作

Java Eclipse 在运行时连续获取命令行参数

java - 查找关联的程序以使用 Java 打开文件

java - 我可以根据 jsr-305 注释对方法参数进行运行时验证吗?