PrintWriter append method not appending

JavaPrintwriter

Java Problem Overview


The following method only writes out the latest item I have added, it does not append to previous entries. What am I doing wrong?

public void addNew() {
    try {
        PrintWriter pw = new PrintWriter(new File("persons.txt"));
        int id = Integer.parseInt(jTextField.getText());
        String name = jTextField1.getText();
        String surname = jTextField2.getText();
        Person p = new Person(id,name,surname);
        pw.append(p.toString());
        pw.append("sdf");
        pw.close();
    } catch (FileNotFoundException e) {...}
}

Java Solutions


Solution 1 - Java

The fact that PrintWriter's method is called append() doesn't mean that it changes mode of the file being opened.

You need to open file in append mode as well:

PrintWriter pw = new PrintWriter(new FileOutputStream(
    new File("persons.txt"), 
    true /* append = true */)); 

Also note that file will be written in system default encoding. It's not always desired and may cause interoperability problems, you may want to specify file encoding explicitly.

Solution 2 - Java

PrintWriter pw = new PrintWriter(new FileOutputStream(new File("persons.txt"),true));

The true is the append flag. See documentation.

Solution 3 - Java

Open the file in append mode, as with the following code:

 PrintWriter pw = new PrintWriter(new FileOutputStream(new File("persons.txt"), true)); 

Solution 4 - Java

IMHO the accepted answer does not consider the fact that the intention is to write characters. (I know the topic is old, but since while searching for the same topic I stumbled upon this post before finding the advised solution, I am posting here.)

From the FileOutputStream docs, you use FileOutputStream when you want to print bytes.

> FileOutputStream is meant for writing streams of raw bytes such as > image data. For writing streams of characters, consider using > FileWriter.

Besides, from the BufferedWriter docs:

> Unless prompt output is required, it is advisable to wrap a > BufferedWriter around any Writer whose write() operations may be > costly, such as FileWriters and OutputStreamWriters.

Finally, the answer would be the following (as mentioned in this other StackOverFlow post):

> PrintWriter out = null; > try { > out = new PrintWriter(new BufferedWriter(new FileWriter("writePath", true))); > out.println("the text"); > }catch (IOException e) { > System.err.println(e); > }finally{ > if(out != null){ > out.close(); > } > } > Also, as of Java 7, you can use a try-with-resources statement. No > finally block is required for closing the declared resource(s) because > it is handled automatically, and is also less verbose: > > try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("writePath", true)))) { > out.println("the text"); > }catch (IOException e) { > System.err.println(e); > }

Solution 5 - Java

You need not to double buffering as shown in all other answers. You can simply do

PrintWriter pw = new PrintWriter(new FileWriter("persons.txt",true));

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsnnlankrdsmView Question on Stackoverflow
Solution 1 - JavaaxtavtView Answer on Stackoverflow
Solution 2 - JavaStephanView Answer on Stackoverflow
Solution 3 - JavaSumit SinghView Answer on Stackoverflow
Solution 4 - JavamarcelocraView Answer on Stackoverflow
Solution 5 - JavaNeeraj KumarView Answer on Stackoverflow