gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:46:17 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com