Click to See Complete Forum and Search --> : writing to a non empty file


dukes
July 23rd, 2007, 03:52 AM
Hi im having some frustrating problems with writing to a file.

what im trying to do is write some formatted data to a file then do some calculations before adding more data to the file. the calculations are not small so its not the best idea to keep the file open in the background like the way RandomAccessFile works. i can get it to write 1 line or a 2nd line that overwrites the 1st. i cant seem to get to add more to the file though.

any help would be greatly apreciated

dlorde
July 23rd, 2007, 05:08 AM
To add data to the end of a file, open it using the FileWriter or FileOutputStream constructors that take a boolean 'append' flag, and pass 'true' as the value:Writer output = new FileWriter("MyFile.txt", true);That is the essence of science: ask an impertinent question, and you are on the way to a pertinent answer...
J. Bronowski

Londbrok
July 23rd, 2007, 05:11 AM
For instance FileWriter class allows you to specify, if you want to write to the end of the file, or overwrite the existing files contents. This is speficied in the constructor as a boolean. False overwrites, true appends.

dukes
July 23rd, 2007, 08:41 PM
ah of course! thanks for the help ive got it working now :)