使用C#讀寫txt文本

創建並寫入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();

結果:
在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章