猿问

JavaFX:如何禁用 TextArea 的多行选项

我有一个从 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 文件的外观:

http://img.mukewang.com/62e094870001181604970301.jpg


慕田峪4524236
浏览 140回答 1
1回答

慕标琳琳

正如您已经提到的,您应该在保存之前转义新行字符,因为它们会破坏您的 csv 文件。您可以使用description.replace("\n", "\\n")在保存description.replace("\\n", "\n")时转义和在加载时取消转义。public void save() throws IOException {&nbsp; &nbsp; try (BufferedWriter bw = new BufferedWriter(new FileWriter(path))) {&nbsp; &nbsp; &nbsp; &nbsp; for (Tasks o : (getTasks())) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bw.write(o.getTask() + ";" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; o.getDeadline().toString() + ";" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; o.getDescription().replace("\n", "\\n"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bw.newLine();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}public void load() throws IOException, ParseException {&nbsp; &nbsp; File file = new File(path);&nbsp; &nbsp; if (file.exists()) {&nbsp; &nbsp; &nbsp; &nbsp; try (BufferedReader br = new BufferedReader(new FileReader(path))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<Tasks> tempTasks = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String line;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ((line = br.readLine()) != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] parts = line.split(";");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String task = parts[0];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LocalDate deadline = LocalDate.parse(parts[1]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String desc = parts[2].replace("\\n", "\n");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tempTasks.add(new Tasks(task, deadline, desc));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tasks.clear();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tasks.addAll(tempTasks);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } else&nbsp; &nbsp; &nbsp; &nbsp; tasks.clear();}但是;在你的标题或描述中使用 a 也会搞砸你的 csv。我建议使用外部库来处理 csv,例如Apache Commons CSV。或者,您可以将任务保存为另一种文本格式,例如 json 或 xml。
随时随地看视频慕课网APP

相关分类

Java
我要回答