我有一个从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 文件的外观:
手掌心
相关分类