我有一个javafx应用程序,我希望从非常复杂的形式添加新记录。表单正常工作,并将数据发送到编写器类以实际将其写入文本文件,但是,新行不会添加到文件中。我怀疑这种行为的原因是该文件已经打开以供阅读以在TableView中显示内容但是我应该抛出一些我没有的异常。 调用writer的控制器方法如下所示:
@FXML
private void handleSubmit() {
String utc = "";
String filePath = mainApp.getAdifFilePath().getPath();
Adif2Writer writer = new Adif2Writer();
// get all records for the left side
try {
for (RecordGridController gridController : controllers) {
Map<String, String> recordData = gridController.getRecordData();
if (recordData.get("call") != null) {
Adif2Record record = new Adif2Record();
utc = recordData.getOrDefault("utc", utc);
String utcFormatted = LocalTime.parse(utc).format(DateTimeFormatter.ofPattern("HHmm"));
record.setCall(recordData.get("callC"));
writer.write(record, filePath);
}
}
// get all records for right side
for (RecordGridController gridController : controllers) {
Map<String, String> recordData = gridController.getRecordData();
if ((recordData.get("rstC") != null) && (recordData.get("rstC").length() > 1)) {
Adif2Record record = new Adif2Record();
utc = recordData.getOrDefault("utc", utc);
String utcFormatted = LocalTime.parse(utc).format(DateTimeFormatter.ofPattern("HHmm"));
record.setCall(recordData.get("callC"));
writer.write(record, filePath);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
编写方法如下:
public void write(Adif2Record record, String filePath) throws IOException {
Adif2Wrapper wrapper = new Adif2Wrapper(record);
BufferedWriter bw = new BufferedWriter(new FileWriter(filePath, true));
bw.write(wrapper.toString());
System.out.println(wrapper.toString());
}
但是当我按下提交按钮时,没有显示异常或警告,但是文件没有更新,但wrapper.toString()会返回正确的文本。