troubleshooting Question

How do I erase everything in my file before adding new stuff?

Avatar of Mickeys
MickeysFlag for Sweden asked on
2 Comments1 Solution163 ViewsLast Modified:
If I run the method below it is just adding stuff to my transaction.txt file. I want it to erase evertyning in it and just add my new stuff. How is this done?
public void writeToFile(String n) {
        FileWriter pw = null;
        try {
            //Skapa en StringBuffer
            StringBuffer buf = new StringBuffer();
            // Lägg till det som ska in i filen.
            buf.append(n).append(" ").append("\r\n");
            //Öppna filen
            File outFile = new File("Transaction.txt");
            JOptionPane.showMessageDialog(null, "Filen sparad som Transaction.txt");
            //Finns filen?
            if (!outFile.exists()) {
            //Skriv sist i filen
            pw = new FileWriter(outFile, true);
        } catch (Exception e) {
            System.err.print("Gick ej öppna. Avslutar");
        } //end catch
        finally {
            //Stäng filen
            if (pw != null) {
                try {
                } catch (IOException e) {
                    System.err.print("Gick ej stänga filen. Avslutar");
                } //end catch
            } //end if
        } //end finnaly
    } //end writeToFile

Open in new window

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros