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();

}

它应该是什么样子:

http://img1.mukewang.com/62e9fc430001a2e805690329.jpg

csv 文件的外观:

http://img4.mukewang.com/62e9fc4f0001ac2b04980298.jpg

侃侃尔雅
浏览 83回答 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,查看更多内容
随时随地看视频慕课网APP

相关分类

Java