創建並寫入txt:
string text = "Test";//也可以寫入StringBuilder等
StreamWriter sw;
FileInfo fi = new FileInfo(Application.dataPath + "//" + "t.txt");
sw = fi.CreateText();
sw.WriteLine(text);//直接重新寫入,追加內容用fi.AppendText()
sw.Close();
sw.Dispose();
讀取txt文本:
StreamReader sr;
sr = File.OpenText(Application.dataPath + "//" + "t.txt");
string s = sr.ReadLine();
if (s != null)
{
Debug.Log(s);
}
sr.Close();
sr.Dispose();
結果: