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

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -