java - PrintWriter to append data if file exist -


I have a savegame file called mysave.sav and I want to add data to this file I want if the file already exists if the file does not exist, then I would like to create a file and then add data.

Adding data works fine but inserted data overwrites existing data. I followed the instructions of exx here ()

string service = "miesaw." File F = new file (waiter); PrintWriter out = new PrintWriter (savestr); If (f.exists () & amp;! F.is directory ()) {out = new print witter (new FileOutputStream (new file (Severstars, true)); Out.append (mapstring); Out.close (); } Other {out.println (mappestring); Out.close (); }

Where there is a mapstrings string that I want to add Can you help me Thanks

Once you call PrintWriter = new PrintWriter (savestr) ;! If the file does not exist, then the file is created, first start it after checking the file existence.

As mentioned in this, as it says:

If the file exists, it will be reduced to zero size; Otherwise, a new file will be created.

Since the file was created before calling f.exists () , it is always true and ìf The block is never executed.

Sample code:

  string savestr = "mysave.sav"; File F = new file (waiter); PrintWriter = Null Out; If (f.exists () & amp;! F.is directory ()) {out = new print witter (new FileOutputStream (new file (Severstars, true)); } And {out = new printers (sewer); } Out.append (mapstring); Out.close ();  

For better resource handling use


Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -