[英]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.