- Home /
Question by
nekologic · Oct 17, 2011 at 12:56 PM ·
javascriptfileio
Unity not properly flushing file output?
So, i've been trying to get Unity to write to some files and the only time the file actually gets created and written is if I generate a file IO exception. If I don't put in bad code which generates an exception the file never actually gets written to disk even though it appears everything went ok. Anyone know what I'm doing wrong here?
function SaveToXML(filename : String) {
var writer : StreamWriter;
var filepath : String;
var info : FileInfo;
filename = filename + ".profile";
filepath = Application.persistentDataPath + "/";
info = new FileInfo(filepath + filename);
//Delete the old file if it exists
if ( info.Exists ) {
info.Delete();
}
//Create new file
writer = File.CreateText(filepath+filename);
//Get Serialized Data and write it
var data : String = Serialize();
writer.Write(data);
writer.Flush();
//Close up shop
writer.Close();
writer.Dispose();
if ( info.Exists ) {
Debug.Log("Verified Existance of file ");
} else {
Debug.Log("Not Verified");
//Lets generate an exception
info = new FileInfo(Application.persistentDataPath);
if ( info.isReadOnly) {
Debug.Log("This will generate an exception");
}
}
Debug.Log("File written " + filepath + filename);
}
Comment
Couldn't you just use File.WriteAllText? It would make things simpler.
Tried File.WriteAllText it does the exact same thing. In fact I did that first and thought I maybe I should try flushing and dispose so that's why I changed it.