我的问题是我填写了一个 csv 文件,但我希望每次运行我的代码清空 csv 然后写入新数据。
每次我运行代码时,都会在 FOR 循环中从我的主体调用此方法...
public static void csv_output() throws Exception {
FileWriter fw = new FileWriter("output/out.csv", true);
//true refers to append
PrintWriter pw = new PrintWriter(fw, false);
//pw.flush();
//clears the Buffer
if (Auctionhouse.current_epoch == 1) {
pw.print("Epoch");
pw.print(",");
for (int j = 0; j < Run_process.total_Agents; j++) {
pw.print(Chromosome.chromosome.get(j).ID + "_Balance");
pw.print(",");
}
for (int k = 0; k < Stockhouse.total_stocks; k++) {
pw.print(Stockhouse.stockP.get(k).stock_Id);
pw.print(",");
}
}
pw.println();
pw.print(Auctionhouse.current_epoch);
pw.print(",");
for (int i = 0; i < Run_process.total_Agents; i++) {
String ag = Portfolio.Agent_balance.get(i) + "";
pw.print(ag);
pw.print(",");
}
for (int j = 0; j < Stockhouse.total_stocks; j++) {
String pr = Auctionhouse.total_prices.get(Auctionhouse.current_epoch - 1).get(j) + "";
pw.print(pr);
pw.print(",");
}
}
pw.flush();
pw.close();
fw.close();
冲洗不起作用。
PS:我想追加,以便我可以在每个循环中写入数据。