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
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

How to add the content to a file?

rose deng
Ranch Hand

Joined: Dec 29, 2000
Posts: 78
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

Joined: Jan 30, 2001
Posts: 13
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
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.
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
subject: How to add the content to a file?
Similar Threads
How can I bypass the download dialogue box while downloading a file.
add timestamp with .txt file & write some information to the .txt file
How to determine if the Http response is a File which needs to be downloaded
MockExam Resources!!
Outputting a text file to a Java GUI component