Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to add the content to a file?

 
rose deng
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic