我有一个可从csv文件写入和读取的应用程序。我的程序中有一个TextArea,但是当我输入一些多行文本时,csv文件损坏了,并且应用无法启动,因为它无法读取它。
这就是我用来保存和加载的内容。
public void save() throws IOException {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(path))) {
for (Tasks o : (getTasks())) {
bw.write(o.getTask() + ";" +
o.getDeadline().toString() + ";" +
o.getDescription());
bw.newLine();
}
}
}
public void load() throws IOException, ParseException {
File file = new File(path);
if (file.exists()) {
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
List<Tasks> tempTasks = new ArrayList<>();
String line;
while ((line = br.readLine()) != null) {
String[] parts = line.split(";");
String task = parts[0];
LocalDate deadline = LocalDate.parse(parts[1]);
String desc = parts[2];
tempTasks.add(new Tasks(task, deadline, desc));
}
tasks.clear();
tasks.addAll(tempTasks);
}
} else
tasks.clear();
}
外观如何:
csv文件的外观: