将字符串添加到txt文件

时间:2016-10-25 20:57:32

标签: java bufferedwriter

所以这是代码

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();
  }
 }

}

它在HospitalManager类中。在主代码中,我有一个该类的实例,我在其上使用addPatient()。问题是,我每次使用该方法时都需要在Patient.txt文件中添加String。我需要将String添加到文件中的新行,但是它只存储addPatient()的第一次使用,第二次使用的方法被简单地忽略,你能告诉我怎么做才能添加新行每次使用addPatient()时使用String?

1 个答案:

答案 0 :(得分:0)

在您close Writer后,您无法再写信了。因此,如果您想再次写入同一文件,则必须每次都打开它

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();
  }
 }
}

或者,您可以保持文件处于打开状态,直到主代码知道它不会添加任何其他患者。

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();
  }
}
}