Java filewriter – attach text line files

I have a button in the GUI. When the button is pressed, the user can add information to the text file I have this part set normally, but what messes me up is that when the user writes a file, it will delete all the information in the text file, and the only line left is the new file just added I need to add information and still keep the original information in the text file I think the append command can do this, but I obviously did something wrong Any help is excellent!

This is my code:

FileWriter fWriter = null;
    BufferedWriter writer = null;
    try {
        fWriter = new FileWriter("info.txt");
        writer = new BufferedWriter(fWriter);


        writer.append(javax.swing.JOptionPane.showInputDialog(this,"add info"));
        writer.newLine();
        writer.close();
    } catch (Exception e) {
    }

Solution

Use a constructor with a bool append parameter See JavaDocs for filewriter

fWriter = new FileWriter("info.txt",true);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>