Path path = Paths.get(access.getFilePath());
Charset charset = StandardCharsets.UTF_8;
String content = new String(Files.readAllBytes(path), charset);
String originalText;
File input = new File(access.getFilePath());
Document doc = Jsoup.parse(input, "UTF-8");
Element htmlElement = doc.select(htmlValue.get(punchLineTextField.getId())).first();
originalText = htmlElement.toString();
htmlElement.text(punchLineTextField.getText());
content = content.replaceAll(originalText, htmlElement.toString());
htmlElement = doc.select(htmlValue.get(newsDatePicker.getId())).first();
originalText = htmlElement.toString();
htmlElement.text(newsDatePicker.getValue().toString());
content = content.replaceAll(originalText, htmlElement.toString());
htmlElement = doc.select(htmlValue.get(welcomeMessageTextArea.getId())).first();
originalText = htmlElement.toString();
htmlElement.text(welcomeMessageTextArea.getText());
content = content.replaceAll(originalText, htmlElement.toString());
Files.write(path, content.getBytes(charset));
TextField 和 DatePicker 值正确保存到文件中(这是一个 HTML 文件)。
但是,由于某种原因,未保存 TextArea 文本。它正确地抓取原始文本但不能正确保存。为什么?
解决方案:替换功能不喜欢文本中的新行。从文本中删除换行符或使用正则表达式在读取文件时删除行。