java - PrintWriter append method not appending -


the following method writes out latest item have added, not append previous entries. 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) {...} } 

the fact printwriter's method called append() doesn't mean changes mode of file being opened.

you need open file in append mode well:

printwriter pw = new printwriter(new fileoutputstream(     new file("persons.txt"),      true /* append = true */));  

also note file written in system default encoding. it's not desired , may cause interoperability problems, may want specify file encoding explicitly.


Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -