Unity 写入文件 读取文件

简单记录一下,不喜勿喷

读取文件方法   

 public void ReadAccount()
    {
        string[] RawString = System.IO.File.ReadAllLines(@"Data\Account.txt");  //路径

        for(int i = 0; i < RawString.Length; i++)     //
        {
            string[] ss = RawString[i].Split(' ');     //截断字节
            Account.Add(ss[0], ss[1]);    //保存到字典里面   自己创建一个字典
        }
    }

写入文件方法

 public static void WriteAccount()
    {
        string text = "";
        foreach(var item in Account)
        {
            text += item.Key + " " + item.Value + "\r\n";   // \r\n 是换行符, 只有\n不会换行
        }
        System.IO.File.WriteAllText(@"Data\Account.txt",text,Encoding.UTF8);
    }

 /写入文件方法

void OnApplicationQuit()
    {
        Debug.Log("执行了么");
        string text = "";//\r\n表示换一行 两行就是\r\n\r\n
        string text0 = "";
        string text1 = "";
        StreamWriter sw = new StreamWriter(Application.streamingAssetsPath + patch, true);
        if (Dicteory_List.instance.Dic_dic.Count != 0)
        {
         
            foreach (var item in Dicteory_List.instance.Dic_dic)
            {                                 
                    text0 = Dic_dic [item.Key].jianjie + "*" + Dic_dic[item.Key].shuxing + "*" + Dic_dic[item.Key].shuxing1 + "*" +
                    Dic_dic[item.Key].cansu + "*" + Dic_dic[item.Key].cansu1 + "*" + Dic_dic[item.Key].danwei + "*" +
                    Dic_dic[item.Key].danwei1 + "*";
                
                text1 += item.Key + "*" + text0 + "\r\n";
            }
           
            text = text1;

            sw.Write(text);
            sw.Flush();
            sw.Close();
        }
    }

3.新加入

3.1定义路径  特殊文件夹打包出来不会压缩StreamingAssets

 string filePath = Application.streamingAssetsPath + "/Resources/"+ _jsonName;

 

3.2 创建文件

StreamWriter sw;
        FileInfo file = new FileInfo(filePath);
        sw = file.CreateText();

3.3 写入文件

 

            StreamWriter sw;
        FileInfo file = new FileInfo(filePath);
        sw = file.AppendText();
            sw.WriteLine("要添加的文本");
            sw.Close();
            sw.Dispose();

3.4 读取文件

     StreamReader sr;
        try
        {
            sr = File.OpenText(filePath);
            string strData = sr.ReadToEnd();
            sr.Close();
        }
        catch
        {
            return null;
        }

4.清空文件内容

方法1

     FileStream fs = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.Write);
    fs.Seek(0, SeekOrigin.Begin);
    fs.SetLength(0);
    fs.Close();

方法2

     FileStream fs = new FileStream(filePath, FileMode.Truncate, FileAccess.ReadWrite);
    fs.Close();

 

一个多类型数据保存本地文件

//数据类型
public class SaveData
{
    public List<string> wordDataList = new List<string>();
    public List<string> photoDataList = new List<string>();
    public int enumTuBiao=-1;
}

//保存数据字典
    public Dictionary<string, SaveData> DicDataid_dic = new Dictionary<string, SaveData>();


/// <summary>
    /// 读取text文件
    /// </summary>
    public void ReadAccount()
    {
        string[] RawString = System.IO.File.ReadAllLines(Application.streamingAssetsPath + "/SaveData/bimSave.txt");  //路径

        for (int i = 0; i < RawString.Length; i++)     //
        {
            SaveData saveData = new SaveData();
            string[] ss = RawString[i].Split('*');     //截断字节
            string[] word = ss[1].Split('%');
            string[] photo = ss[2].Split('!');           
            if (word.Length >0)
            {
                for (int k = 0; k < word.Length; k++)
                {
                    if(word[k]!="")
                    saveData.wordDataList.Add(word[k]);
                }            
            }
            if (photo .Length >0)
            {
                for (int k = 0; k < photo.Length; k++)
                {
                    if (photo[k] != "")
                        saveData.photoDataList.Add(photo[k]);
                }
            }
            if (ss[3]!=null)
            {
                saveData.enumTuBiao =int.Parse ( ss[3]);
            }
            DicDataid_dic.Add(ss[0], saveData);        
        }
        Debug.Log(DicDataid_dic.Count);
       
        FileStream fs = new FileStream(Application.streamingAssetsPath + "/SaveData/bimSave.txt", FileMode.Truncate, FileAccess.ReadWrite);
       //清空文件数据
        fs.Close();
    }

    /// <summary>
    /// 程序退出的时候保存数据
    /// </summary>
    private void OnApplicationQuit()
    {

        //\r\n表示换一行 两行就是\r\n\r\n
        string text0 = "";
        string text1 = "";
        string text2 = "";
       
        StreamWriter sw = new StreamWriter(Application.streamingAssetsPath + "/SaveData/bimSave.txt", true);
        if (DicDataid_dic.Count != 0)
        {
            foreach (var item in DicDataid_dic)
            {
                for (int i = 0; i < DicDataid_dic[item.Key].wordDataList.Count; i++)
                {
                    text0 += DicDataid_dic[item.Key].wordDataList[i] + "%";
                }
                for (int i = 0; i < DicDataid_dic[item.Key].photoDataList.Count; i++)
                {
                    text2 += DicDataid_dic[item.Key].photoDataList[i] + "!";
                }

                text1 = item.Key + "*" + text0 + "*" + text2 + "*" + DicDataid_dic[item.Key].enumTuBiao + "*";
                //一行一行的写入
                sw.WriteLine(text1);
                text0 = "";
                text2 = "";
                text1 = "";             
            }         
            sw.Flush();
            sw.Close();
            Debug.Log("执行了么");
        }
    }

 

根据文件路径删除文件

  File.Delete(_path);

 

 

 

posted @ 2020-10-15 10:20  剑起苍穹  阅读(2830)  评论(0编辑  收藏  举报
/*鼠标点击特效*/