创建本地文件,需要使用FileInfo类,在构造方法中写入文件的保存路径,通过FileInfo的对象使用CreateText方法,可在本地创建一个文本文件
使用AppendText方法打开已经创建的文本,最后WriteLine可以将字符串写入文本
StreamReader sr =File.OpenText(path + "//" + name);
打开文本,sr.ReadLine()进行读取
void Start () {
CreateFile (Application.dataPath,"FileName","TestInfo0");
CreateFile (Application.dataPath,"FileName","TestInfo1");
CreateFile (Application.dataPath,"FileName","TestInfo2");
ArrayList arrlist = LoadFile(Application.dataPath,"FileName");
foreach (string str in arrlist) {
Debug.Log("--------------------" + str);
}
}
/**将文本写入文件*/
void CreateFile(string path,string name,string info)
{
StreamWriter sw;
FileInfo t = new FileInfo (path + "//" + name);
if (!t.Exists)
{
sw = t.CreateText ();
} else
{
sw = t.AppendText();
}
sw.WriteLine (info);
sw.Close ();
sw.Dispose ();
}
/**文件的读取*/
ArrayList LoadFile(string path,string name)
{
StreamReader sr = null;
try{
sr = File.OpenText(path + "//" + name);
}
catch(Exception e)
{
Debug.Log("没有文件" + e.Message);
return null;
}
string line;
ArrayList arrList = new ArrayList ();
while ((line = sr.ReadLine()) != null) {
arrList.Add(line);
}
sr.Close ();
sr.Dispose ();
return arrList;
}