我在 TextArea 中有一些文本,我想将其保存在文件中,我的代码在这里:
private void SaveFile() {
try {
String content = txt.getText();
File file = new File(filename);
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
但它在没有“\n”的情况下保存;在新文件中,一切都在一行上;我也能预见到那些“进入”吗?先感谢您
问题是因为记事本,所以这里是解决方案:
private void SaveFile() {
try {
String content = txt.getText();
content = content.replaceAll("(?!\\r)\\n", "\r\n");
File file = new File(filename);
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
感谢您的帮助