0

I have a BufferedWriter which writes into a file already, but not in the way I want to. I initialize it within a loop, causing it to append only the last result of the loop to get added to the file. Actually this is ok for me but the previous results in the file get deleted. I want the result getting added without deleting the previous results.

I tried to create static Buffered Reader in order to not always create new instances of Buffered Reader in the loop. But this is not possible since you need a "try" block.

public void actionPerformed(ActionEvent e) {
            String s;
            try {
                if (parseInt(jTextFieldNumber.getText()) == random) {
                    info.setText("Attempt # " + counter + ": " + 
parseInt(jTextFieldNumber.getText()) + " ==> correct!");
                    ok.setEnabled(false);
                } else if (parseInt(jTextFieldNumber.getText()) < random) 
{
                    info.setText("Attempt # " + counter + ": " + 
parseInt(jTextFieldNumber.getText()) + " ==> too small!");
                } else {
                    info.setText("Attempt # " + counter + ": " + 
parseInt(jTextFieldNumber.getText()) + " ==> too big!");
                }
            } catch (NumberFormatException nfe) {
                nfe.printStackTrace();
                System.exit(0);
            }
                s = jTextFieldName.getText() + " " + counter;
                BufferedWriter bw;
                try {
                    bw = new BufferedWriter(new 
FileWriter("C:\\wrk\\SapCommerceCloud\\src\\SwingUni\\events1\\name"));
                    bw.append(s);
                    if(info.getText().equals("Attempt # " + counter + ": 
" + parseInt(jTextFieldNumber.getText()) + " ==> correct!")){
                        bw.close();
                    }
                } catch (IOException io) {
                    io.printStackTrace();
                }
            counter++;
        }
    };
baao
  • 71,625
  • 17
  • 143
  • 203
David Baur
  • 13
  • 3
  • 1
    Possible duplicate of [How to append text to an existing file in Java](https://stackoverflow.com/questions/1625234/how-to-append-text-to-an-existing-file-in-java) – Balderk May 21 '19 at 10:46

3 Answers3

1

I think you are asking how to append to a file, even though you are creating new BufferedWriters.

Use file writer with the second parameter as true, like so:

try {
    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("myfile.txt", true)));
    out.println("the text");
    out.close();
} catch (IOException e) {
    //exception handling left as an exercise for the reader
}

Refer: How to append text to an existing file in Java

Teddy
  • 4,009
  • 2
  • 33
  • 55
0

Use

 bw = new BufferedWriter(new 
FileWriter("C:\\wrk\\SapCommerceCloud\\src\\SwingUni\\events1\\name", true));

Call this constructor Test

FileWriter(String fileName, boolean append)
Constructs a FileWriter object given a file name with a boolean indicating whether or not to append the data written.
XRP DEV
  • 32
  • 3
0

FileWriter has a constructor public FileWriter(String fileName, boolean append) throws IOException. If you use true as append, the File won't get overwritten see the javadoc of FileWriter

A better solution for you may be to use one FileWriter.

dan1st
  • 12,568
  • 8
  • 34
  • 67