亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

JavaFX:如何禁用TextArea的多行選項

JavaFX:如何禁用TextArea的多行選項

侃侃爾雅 2022-08-03 12:39:41
我有一個從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 文件的外觀:
查看完整描述

1 回答

?
手掌心

TA貢獻1942條經驗 獲得超3個贊

您應該在保存之前轉義新的行字符,因為它們會破壞您的csv文件,正如您已經提到的。


您可以使用在保存時轉義,并在加載時取消逃逸。description.replace("\n", "\\n")description.replace("\\n", "\n")


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().replace("\n", "\\n"));

            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].replace("\\n", "\n");

                tempTasks.add(new Tasks(task, deadline, desc));

            }

            tasks.clear();

            tasks.addAll(tempTasks);

        }

    } else

        tasks.clear();

}

但是在你的標題或描述中使用a也會搞砸你的csv。;


我建議使用外部庫進行csv處理,例如Apache Commons CSV?;蛘?,您可以將任務保存為其他文本格式,如 json 或 xml。


查看完整回答
反對 回復 2022-08-03
  • 1 回答
  • 0 關注
  • 96 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號