Unity写入和读取文本文件

Unity写入和读取文本文件

打开文件并写入:

void CreateOrOPenFile(string path, string name, string info){          //路径、文件名、写入内容
 StreamWriter sw; 
 FileInfo fi = new FileInfo(path + "//" + name);
 sw = fi.CreateText ();        //直接重新写入,如果要在原文件后面追加内容,应用fi.AppendText()
 sw.WriteLine(info);
 sw.Close();
 sw.Dispose ();
}

读取文件内容:

void LoadFileAndIntialSpeed (string sPath, string sName){
 StreamReader sr = null;
 sr = File.OpenText (sPath + "//" + sName);

 string t_Line;

 if ((t_sLine = sr.ReadLine ()) != null) {

  //do some thing with t_sLine

 } else {
 print ("Null!");
 }

 sr.Close ();
 sr.Dispose ();
}

你可能感兴趣的:(Unity3D)