Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Opening files in append mode  RSS feed

 
M Burke
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I do this...
BufferedWriter log = new BufferedWriter(new FileWriter(LOGNAME));

it destroys the file if it exists. How can I open a file and write to it in append mode?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JavaDoc is your friend ... take a look at other constructors for FileWriter.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at the API, scroll down to the constructors of Filewriter()
(you'll notice one with a boolean value)

http://java.sun.com/j2se/1.4.2/docs/api/java/io/FileWriter.html
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by M Burke:
When I do this...
BufferedWriter log = new BufferedWriter(new FileWriter(LOGNAME));

it destroys the file if it exists. How can I open a file and write to it in append mode?


You can use ::

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.


Example

 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the I/O and Streams forum...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!