繁体   English   中英

将字符串添加到txt文件

[英]Adding Strings to txt file

所以这是代码

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()第一次使用,方法的第二次使用和其他使用都被忽略,请您告诉我该如何使用String添加新行每次使用addPatient()吗?

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

暂无
暂无

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

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