wood burning stoves 2.0*
The moose likes Java in General and the fly likes How to add the content to a file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to add the content to a file?" Watch "How to add the content to a file?" New topic
Author

How to add the content to a file?

rose deng
Ranch Hand

Joined: Dec 29, 2000
Posts: 78
Hi.
What i want to do is open a file and add content to it,not overwrite previous content.
Thanks for your answer.
Have a nice evening.
Shripad Bokil
Greenhorn

Joined: Jan 30, 2001
Posts: 13
Hi
What you can do is when you open a file, read it & store in a String variable & while writing in that file just write the same string with appending it by what you want to add in that string.
Hope it works.
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
Copied from JDK doc
FileOutputStream
public FileOutputStream(String name,
boolean append)
throws FileNotFoundException
Creates an output file stream to write to the file with the specified name. If the second argument is true, then bytes will be written to the end of the file rather than the beginning. A new FileDescriptor object is created to represent this file connection.
First, if there is a security manager, its checkWrite method is called with name as its argument.
If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason then a FileNotFoundException is thrown.
Parameters:
name - the system-dependent file name
append - if true, then bytes will be written to the end of the file rather than the beginning
Chris Stehno
Ranch Hand

Joined: Feb 26, 2001
Posts: 180
The java.io.FileWriter also has the same feature in one of its constructors ... new FileWriter("myfile.txt",true) will append to the file rather than overwrite it.
You may also want to look at RandomAccessFile though I don't recommend it ... I don't think that class is very friendly.
------------------
Chris Stehno (Sun Certified Programmer for the Java 2 Platform)


- Chris Stehno, SCPJ
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to add the content to a file?