我写了一种自动取款机程序,将数据输出到一个文件中(是的,我知道它不是英语,但是这不是重点),我遇到了一个错误。
当我尝试使用PrintWriter
它不工作,我不知道为什么。
public void writeFooter(List<Purchase> list) throws Exception{
int amountSold = 0;
int amountNotSold = 0;
int moneyRecieved = 0;
openStreams();
printer.println("Проданные товары: ");
for(int i = 0; i <= list.size() - 1;i++){
if(list.get(i).isSold()){
writeToFile(list.get(i), dir);
amountSold++;
moneyRecieved += list.get(i).getPrice();
}
}
printer.println();
printer.println("Не проданные товары: ");
for(int i = 0; i <= list.size() - 1; i ++){
if(!list.get(i).isSold()){
writeToFile(list.get(i), dir);
amountNotSold++;
}
}
printer.println();
printer.println("Всего: "+list.size());
printer.println("Кол-во проданно: "+ amountSold);
printer.println("Кол-во не проданно: "+ amountNotSold);
printer.println("Выручка: "+ moneyRecieved);
printer.flush();
System.out.print(printer.checkError());
closeStreams();
}
private void openStreams() throws IOException{
writer = new FileWriter(file,true);
buffer = new BufferedWriter(writer);
printer = new PrintWriter(buffer);
}
private void closeStreams() throws IOException{
printer.flush();
printer.close();
buffer.close();
writer.close();
}
public void writeToFile(Purchase purchase,String dir) throws Exception{
file = new File(dir);
if(!file.exists()){
file.createNewFile();
}
openStreams();
printer.println(purchase.getName() + " По цене: " + purchase.getPrice() + "руб");
closeStreams();
}
在for循环工作,但线。 这真的让我困惑! 我曾尝试checkError(
我得到true
,但仍然于事无补。
有人可以解释我到底做错了什么?