Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Writing to a file and adding on
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Aziz




PostPosted: Wed Jul 06, 2005 2:45 pm   Post subject: 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:)
code:

while (!word == null)
{
     currentText += readLine()
}

newText = currentText + textToAdd

println(newText)
Sponsor
Sponsor
Sponsor
sponsor
1of42




PostPosted: Wed Jul 06, 2005 3:09 pm   Post subject: (No subject)

Use the optional second boolean argument to the FileWriter constructor - set it to true to append to the file instead of truncating it.
wtd




PostPosted: Wed Jul 06, 2005 4:03 pm   Post subject: (No subject)

To make it even easier, the Java API reference page on FileWriter.
Aziz




PostPosted: Wed Jul 06, 2005 7:08 pm   Post subject: (No subject)

Thanks a bunch guys, helped out a bunch Smile
Spartan_117




PostPosted: Fri Jul 08, 2005 12:17 pm   Post subject: (No subject)

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




PostPosted: Sat Jul 09, 2005 1:55 am   Post subject: (No subject)

Spartan_117 wrote:
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




PostPosted: Sat Jul 09, 2005 9:48 am   Post subject: (No subject)

Well, of course, I always (which means, usually, when I remember) call the close() method Razz (at least I hope I did....*paranoid* let me double-check...)
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: