1

我正在编写一种自动取款机程序,它将数据输出到一个文件中(是​​的,我知道它不是英文的,但这不是重点),我遇到了一个错误。

当我尝试使用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了,但这仍然没有帮助。

有人可以解释我做错了什么吗?

4

2 回答 2

0

为什么在 writeToFile 调用中打开和关闭流?您不应该在 for 循环之前在 writeFooter() 中打开流,在编写代码周围放置一个 try 块,然后在 finally 部分中关闭它们。

在我看来,您首先在 writeFooter 中打开了流,然后您编写了 printer.println("Проданные товары:");,然后您的 for 循环的第一次迭代调用 writeToFile,将再次打开未关闭的流,即肯定会覆盖第一行打印。

打开和关闭文件以写入一行效率太低了。

你需要类似的东西

writeFooter(...) {

  try {
    openStreams();
    writeToFile();
  } finally {
    closeStream();
  }
}

writeToFile 简单地写入,不打开或关闭流,而 closeStream 安全地关闭流,即检查 null 等。

于 2012-12-06T14:31:10.070 回答
0
public static void studentAdd() throws IOException ,ClassNotFoundException{

    DataOutputStream output = new DataOutputStream(new FileOutputStream("Student.txt"));
    PrintWriter pr = new PrintWriter("Student.txt");
    Student7 student[] = new Student7[total];
    int STOP = 999;
    int stdID;
    int age;
    String address;
    String gender;
    String name;

    Scanner input = new Scanner(System.in);
    System.out.println("Please type  student ID or 999 to quit");
    stdID = input.nextInt(); 

    try {
        while(stdID != STOP) {  
            input.nextLine();
            output.writeInt(stdID);
            pr.print(stdID + "#");

            System.out.println("Please type student name");
            name = input.nextLine();
            output.writeUTF(name);
            pr.print(name + "#");

            System.out.println("Please type student age");
            age = input.nextInt();
            output.writeInt(age);
            input.nextLine();
            pr.print(age + "#");

            System.out.println("Please type student gender");
            gender = input.nextLine();
            output.writeUTF(gender);
            pr.print(gender + "#");                     

            System.out.println("Please type student address");
            address = input.nextLine();
            output.writeUTF(address);
            pr.print(address + "#");
            pr.println();

            System.out.println("Please type  student ID or 999 to quit");
            stdID = input.nextInt();
            }
        ;   pr.close();
            output.close();

    } catch (IOException e) { 
        System.err.println("Error is" + e.getMessage());
    } catch(InputMismatchException e) {
        System.err.println("Invalid Data Entry");
    }finally {
        studentMenu();
    }

} //end of stuadd
于 2013-02-05T02:54:59.667 回答