- Home /
Problems with writing to a file
Hi, I am using JavaScript in order to write some game statuses to a text file.
But for some reason, I can't write to them.
My code goes something like:
import System.IO;
import System;
Var filename : string;
Function Start()
{
Var sw = File.Create(filename);
Sw. Write("Test");
}
The weird bit is that the text file is created BUT when I open it, the text isn't there.
I have tried the Stream Builder method and that didn't work so I presume its something wrong with the Sw.Write.
I'm honestly considering having a second script done in C# for writing to the file. Is that advisable as well?
Thank you
Error outputs help. Remember that everytime you do something with a file you always have to close it properly using Close() Check
var sw : StreamWriter = new StreamWriter(filepathIncludingFileName);
sw.WriteLine("Line to write");
sw.WriteLine("Another Line");
sw.Flush();
sw.Close();
from http://forum.unity3d.com/threads/can-i-read-and-write-text-files-using-javascript.5084/
Google: https://www.google.com/search?q=unity+javascript+write+to+file
You think that may be the problem here? Okay. I'll give it a shot. Then if that's so, then thanks.
You don't really have to close it until you close the application / when you destroy the object writing to it (OnApplicationQuit / OnDestroy)
When you check the documentation you will find out that writing usually writes into a buffer and to ensure that the stuff in the buffer is actually written you have to flush it.
Your answer