
-----------------------------------
Aziz
Wed Jul 06, 2005 2:45 pm

Writing to a file and adding on
-----------------------------------
When I write to a file, it erases the whole file, right? Is there a way to simply add to the end of a file? Or do I have to do something like this:\

(Psuedo:)

while (!word == null)
{
     currentText += readLine()
}

newText = currentText + textToAdd

println(newText)


-----------------------------------
1of42
Wed Jul 06, 2005 3:09 pm


-----------------------------------
Use the optional second boolean argument to the FileWriter constructor - set it to true to append to the file instead of truncating it.

-----------------------------------
wtd
Wed Jul 06, 2005 4:03 pm


-----------------------------------
To make it even easier, [url=http://java.sun.com/j2se/1.5.0/docs/api/java/io/FileWriter.html]the Java API reference page on FileWriter.

-----------------------------------
Aziz
Wed Jul 06, 2005 7:08 pm


-----------------------------------
Thanks a bunch guys, helped out a bunch :)

-----------------------------------
Spartan_117
Fri Jul 08, 2005 12:17 pm


-----------------------------------
I believe the reason it does that is becuase once u write to the file u arent closing the file (or the IO stream) if u close it it should work.. But its been a while so im not a hundred percent

-----------------------------------
1of42
Sat Jul 09, 2005 1:55 am


-----------------------------------
I believe the reason it does that is becuase once u write to the file u arent closing the file (or the IO stream) if u close it it should work.. But its been a while so im not a hundred percent

Nope. The reason it's truncating the file is that you have to add the boolean append argument set to true for it to add to the file instead of truncate/write a new one.

Although, if he doesn't close (or flush, or checkError()) the stream, he won't get any data written at all.

-----------------------------------
Aziz
Sat Jul 09, 2005 9:48 am


-----------------------------------
Well, of course, I always (which means, usually, when I remember) call the close() method :P (at least I hope I did....*paranoid* let me double-check...)
