[英]PrintWriter not writing to file (Java)
我正在编写一种自动提款机程序,该程序会将数据输出到文件中(是的,我知道它不是英语,但这不是重点),并且遇到了错误。
当我尝试使用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
,但这仍然无济于事。
有人可以解释我在做什么错吗?
为什么要在writeToFile调用中打开和关闭流? 您不应该在for循环之前在writeFooter()中打开流,在编写代码的周围放一个try块,然后在finally节中关闭它们。
以我的方式来看,您首先在writeFooter中打开了流,然后您编写了printer.println(“Проданныетовары:”);,然后for循环的第一次迭代调用writeToFile,将再次打开未关闭的流,即肯定会覆盖第一行。
打开和关闭用于写入一行的文件效率太低。
你需要类似的东西
writeFooter(...) {
try {
openStreams();
writeToFile();
} finally {
closeStream();
}
}
writeToFile只是简单地写入,不会打开或关闭流,而closeStream安全地关闭流,即检查null等。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.