我似乎无法弄清楚如何使用JavaFX并接受一定数量的条目将数据存储在文本文件的TextField中。例如:一个人将填写3次表格,所有这3条信息都将在txt文件中。如何在方法中实现ArrayList以便显示?
我已经尝试实现String ArrayList,但是当我按“ Save Information”时,它不会在TextFields中显示数据,所有显示的是[, , , ]
public void saveInfo(ActionEvent e) {
ArrayList<String> list = new ArrayList<>();
File fileIt = new File("InfoGathered.txt");
try {
PrintWriter output = new PrintWriter(fileIt);
for (int i = 0; i < ; i++) {
String s1 = new String();
output.println(tfFirstName.getText() + tfLastName.getText() + tfdBirth.getText() + tfEmpID.getText());
list.add(s1);
}
output.write(list.toString());
output.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
我希望TextFields出现在文件中,例如[Sam Smith 12/03/94 123-AB, Lena Smith 12/12/91 127-AB, Sam Smith 02/18/95 726-HF ]
答案 0 :(得分:0)
您的代码中有很多根本上错误的地方,我什至不知道从哪里开始。但是,如果这是您要解决的特定问题的解决方案,则下面的代码将以所需的格式将TextFields的文本写入文件中。
public void saveInfo(ActionEvent e) {
File fileIt = new File("InfoGathered.txt");
try (PrintWriter output = new PrintWriter(fileIt)){
String outString = tfFirstName.getText() + " "
+ tfLastName.getText() + " "
+ tfdBirth.getText() + " "
+ tfEmpID.getText();
output.write(outString);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}