我正在编写一个记事本克隆以了解有关 guis 的更多信息。我的程序使用以下代码创建一个文本文件:
try {
formatter = new Formatter(fileName+".txt");
formatter.format(contents);
formatter.close();
JOptionPane.showMessageDialog(null, "File saved as "+fileName +".txt");
} catch (Exception e){
JOptionPane.showMessageDialog(null, "Error writing to file");
}
如何保持文本的格式?我正在从 JTextArea 中检索文件:
String contents = text.getText();
但基本上我失去了所有格式。当我从文件中读取时,单词之间会出现空格:
String reading = sc.next(); 已打开文本 = 已打开文本 + " " + 阅读;
无论如何将格式存储在字符串中?