- Home /
Writing to a File Without Deleting What's Already There
I have this script that I want to not override whats in a file already but just append the text to the end.
function WriteToFile(){
var file = StreamWriter(Application.dataPath + "/myFile.txt");
file.Write("?");
file.Close();
}
Like I said how do I make it so that the "?" gets written to the end of the file instead of deleting what's there then writing it?
Answer by Graham-Dunnett · Oct 30, 2011 at 08:05 PM
Searching with google (for msdn StreamWriter append) gave me: http://msdn.microsoft.com/en-us/library/36b035cb.aspx. Does that help?
Answer by racheltkj47 · Sep 13, 2015 at 02:11 PM
I can't think of a way to add stuff at the end of the line but to add a new line u use Console.WriteLine method. It doesn't override.
Otherwise u need to read those already exist in the file and covert them to strings then edit. To do that u use the StreamReader.ReadLine method abd remember to write them back in the file.
Hope this can help u, cheers!
Have a look at the date, it's a question from 2011 :) And there's already an answer which works. You don't have to read everything into memory in order to append something.
Yes i knew:) but i am having the same question recently, just want to leave a note for future me in case i forget what i learnt so i can google it to find my own answer.