简体   繁体   中英

Adding Strings to txt file

So here's the code

public class HospitalManager {
  Writer write = new FileWriter("C:\\Users\\Tigra\\Desktop\\TikDevExp\\Patient.txt");
  FileWriter fw = new FileWriter("Patient.txt");
  BufferedWriter bw = new BufferedWriter(fw);

  public HospitalManager() throws IOException {
  }

  public Patient registerPatient(Patient p1) {
    out.println("=Adding new patient=");
    out.println("Please enter the name");
    Scanner setter = new Scanner(System.in);
    p1.name = setter.nextLine();
    out.println("Enter the surname");
    p1.surName = setter.nextLine();
    out.println("Enter the diagnosys");
    p1.diagnose = setter.nextLine();

    return p1;
}

public void addPatient(Patient addThisOne, List<String> patientList) {
  try {
    bw.append(addThisOne.name + ", " + addThisOne.surName + ", " + addThisOne.diagnose);
    bw.newLine();
    bw.close();
  } catch (IOException e){
    e.printStackTrace();
  }
 }

}

its in HospitalManager class. In main code I have an instance for that class, and I use addPatient() on it. The thing is, I need it to add String in Patient.txt file each time I use that method. I need String to be added to new line in file, but instead, it stores only first use of addPatient() , second and further uses of method being simply ignored, can you please tell me what can I do to add new line with String each time addPatient() is used?

After you close a Writer , you can't write to it anymore. So, if you want to write to the same file again, you will have to open it each time

public class HospitalManager {
  // The Writer write isn't used in your code
  // fw and bw will be declared in addPatient

  public HospitalManager() { // No IOExceptions here anymore
  }

  public Patient registerPatient(Patient p1) {
    out.println("=Adding new patient=");
    out.println("Please enter the name");
    Scanner setter = new Scanner(System.in);
    p1.name = setter.nextLine();
    out.println("Enter the surname");
    p1.surName = setter.nextLine();
    out.println("Enter the diagnosys");
    p1.diagnose = setter.nextLine();

    return p1;
}

public void addPatient(Patient addThisOne, List<String> patientList) {
  // Declare variables outside of the block so they can be
  // referenced in finally
  FileWriter fw;
  BufferedWriter bw;
  try {
    fw = new FileWriter("Patient.txt");
    bw = new BufferedWriter(bw);
    bw.append(addThisOne.name + ", " + addThisOne.surName + ", " + addThisOne.diagnose);
    bw.newLine();
    bw.close();
  } catch (IOException e){
    e.printStackTrace();
  }
 }
}

Alternatively, you could keep the file open until the main code knows that it won't add any other patients.

public class HospitalManager {
  FileWriter fw = new FileWriter("Patient.txt");
  BufferedWriter bw = new BufferedWriter(fw);

  public HospitalManager() throws IOException {
  }

  public Patient registerPatient(Patient p1) {
    out.println("=Adding new patient=");
    out.println("Please enter the name");
    Scanner setter = new Scanner(System.in);
    p1.name = setter.nextLine();
    out.println("Enter the surname");
    p1.surName = setter.nextLine();
    out.println("Enter the diagnosys");
    p1.diagnose = setter.nextLine();

    return p1;
}

public void addPatient(Patient addThisOne, List<String> patientList) {
  try {
    bw.append(addThisOne.name + ", " + addThisOne.surName + ", " + addThisOne.diagnose);
    bw.newLine();
    // Don't close the file, but write the contents of the buffer
    bw.flush();
  } catch (IOException e) {
    e.printStackTrace();
  }
 }

// Call this when no more patients are going to be added
public void done() {
  try {
    bw.close();
  } catch (IOException e) {
    e.printStackTrace();
  }
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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