我想把订单写入档案。例如,如果文件中的订单及其订单号为1,然后当用户添加另一个订单并点击保存时,订单将保存为.txt文件,作为该日期的订单号2。但是我的fileWriter写入了与订单号1相同的文件中的文件。请看看我的DaoImpl。谢谢。
private void writeOrder(LocalDate date) throws OrderPersistenceException, IOException {
PrintWriter out;
try {
out = new PrintWriter(new FileWriter(getFilePath(date)));
} catch (IOException ex) {
throw new OrderPersistenceException("file");
}
List<Order> orderList = new ArrayList<>(orderMap.values());
for (Order currentOrderList : orderList) {
out.print(currentOrderList.getOrderNumber() + DELIMITER
+ currentOrderList.getCustomerName() + DELIMITER
+ currentOrderList.getState() + DELIMITER
+ currentOrderList.getTaxRate() + DELIMITER
+ currentOrderList.getProductType() + DELIMITER
+ currentOrderList.getArea() + DELIMITER
+ currentOrderList.getCostPerSquareFoot() + DELIMITER
+ currentOrderList.getLaborCostPerSquareFoot() + DELIMITER
+ currentOrderList.getMeterialCost() + DELIMITER
+ currentOrderList.getTotalTax() + DELIMITER
+ currentOrderList.getTotal());
out.flush();
}
out.close();
}
public void saveWork(LocalDate date) throws OrderPersistenceException, IOException {
writeOrder(date);
}
@Override
public Order addOrder(Order order) throws OrderPersistenceException {
long orderNumber = ordernumberIncrease(order);
Order newOrder = orderMap.put(orderNumber, order);
return newOrder;
}
public Long ordernumberIncrease(Order order) {
long orderNumber = orderNumInc++;
order.setOrderNumber(orderNumber);
return orderNumber;
}
答案 0 :(得分:0)
您需要更新writeOrder方法:
在for循环使用out.println而不是out.print - 然后每个订单将从新行开始。