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

时间:2015-03-17 17:46:44

标签: 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文件看起来像这样(注意顶部没有增量编号,这导致错误):

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文件的顶部而不是我想要的底部。

1 个答案:

答案 0 :(得分:1)

您需要使用FileWriter构造函数,该构造函数接受一个布尔参数,指示是否附加到该文件。

  

public FileWriter(String fileName,              布尔附加)               抛出IOException

     

在给定带有布尔值的文件名的情况下构造FileWriter对象   指示是否附加所写的数据。