简体   繁体   English

PrintWriter 不写入 Java 中的文本文件

[英]PrintWriter does not write into text file in Java

I'm trying to write an arraylist into a text file using print writer.我正在尝试使用打印编写器将 arraylist 写入文本文件。

Here is the code这是代码

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    if(tfVaccineType.getText() == "sinovac" || tfVaccineType.getText() == "pfizer" || tfVaccineType.getText()=="astrazenecca" || tfVaccineType.getText() == "johnson" && tfVaccineCentre.getText().equals(VaccinationCentre.bukitjalil) || tfVaccineCentre.getText().equals(VaccinationCentre.klcc) || tfVaccineCentre.getText().equals(VaccinationCentre.sripetaling) || tfVaccineCentre.getText().equals(VaccinationCentre.cheras)){
       String vaccineType = tfVaccineType.getText();
       String vaccineCentre = tfVaccineCentre.getText();
       VaccinationCentre vc = VaccinationCentre.valueOf(vaccineCentre);
       int vaccineStock = (Integer)vaccineSpinner.getValue();
       Vaccine v = new Vaccine(vaccineType,vc,vaccineStock);
       DataIO.vaccine.add(v);
       DataIO.write();
    }else{
        System.out.println("error");
    }// TODO add your handling code here:
}

That's the function in my button when I action performed and here is the print writer function这是我执行操作时按钮中的 function,这是打印编写器 function

public static void write(){
    try{
        PrintWriter v = new PrintWriter("vaccine.txt");
        for(int i=0; i<vaccine.size(); i++){
            v.println(vaccine.get(i).getVaccineType());
        v.println(vaccine.get(i).getVaccineCentre());
        v.println(vaccine.get(i).getVaccineStock());
        v.println();
        }
        v.close();
    } catch(Exception e){
        System.out.println("Error in write!");
    }
}

When I run the code, it doesn't produce an error in write, but when I try using System.out.println in my button function it is not even printed to the console.当我运行代码时,它不会产生写入错误,但是当我尝试在我的按钮 function 中使用 System.out.println 时,它甚至不会打印到控制台。 Even when I try without using if else, it's not working either.即使我尝试不使用 if else,它也不起作用。 Is there a way to fix this?有没有办法来解决这个问题?

Try the following if in method jButton1ActionPerformed if在方法jButton1ActionPerformed中尝试以下操作

String vaccineType = tfVaccineType.getText();
if("sinovac".equals(vaccineType) || "pfizer".equals(vaccineType) || "astrazenecca".equals(vaccineType) || "johnson".equals(vaccineType)){
    String vaccineCentre = tfVaccineCentre.getText();
    if (vaccineCentre.equals(VaccinationCentre.bukitjalil) || vaccineCentre.equals(VaccinationCentre.klcc) || vaccineCentre.equals(VaccinationCentre.sripetaling) || vaccineCentre.equals(VaccinationCentre.cheras)) {
       VaccinationCentre vc = VaccinationCentre.valueOf(vaccineCentre);
       int vaccineStock = (Integer)vaccineSpinner.getValue();
       Vaccine v = new Vaccine(vaccineType,vc,vaccineStock);
       DataIO.vaccine.add(v);
       DataIO.write();
    }else{
        System.out.println("error");
    }
}else{
    System.out.println("error");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM