我一直在尝试几个星期才能使此程序工作,但没有成功。它是一个在SwingUI中创建的简单程序,带有多个文本字段和组合框。我想将输入到这些字段的数据保存到文本文件中。我能够从FName字段获取数据,但没有其他。也许这里有人能带领我走向正确的方向?将数据从多个jtextfields和组合框保存到.TXT文件
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
JOptionPane.showMessageDialog(frame, "Thank you for joining BPA! Your information has been submitted.");
String content = FName.getText();
LName.getText();//step1: get the content of the textfield
try {
File file = new File("c:/users/User/Documents/BPASignup.txt");
// 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); //step2: write it
bw.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
非常感谢!我得到了它的工作。如何在文本文件中的每个字符串之间添加空行? –
'\ n'是换行符。如果您需要,只需添加更多。 –
因此,它已经应该添加一个新的代码,你给我看?它不工作。当我打开文本文档时,所有内容仍然保持一致。 –