天天看點

unity3d學習筆記 對檔案得讀取與寫入

//讀取檔案夾内所有txt檔案

foreach (string subFile in Directory.GetFiles(PathHelper.AppHotfixResPath))
            {
                if (Path.GetExtension(subFile) == ".txt")
                {
                    string name = Path.GetFileNameWithoutExtension(subFile);
                    if(name!= "Version")
                    {
                        FileStream aFile = new FileStream(subFile, FileMode.Open);
                        StreamReader sr = new StreamReader(aFile);
                        string text = sr.ReadToEnd();
                        ConfigDir.Add(name, text);
                        sr.Close();
                        aFile.Close();
                    } 
                }
           

//存儲檔案

private static void SaveMapJson(string mapName, string Json_info)
    {

        string exportPath = Path.Combine(PathHelper.AppHotfixResPath, mapName + ".txt");//獲得路徑
        if (File.Exists(exportPath))
        {
            File.Delete(exportPath);
        }
        using (FileStream fs = new FileStream(exportPath, FileMode.Create))
        {
            StreamWriter sw = new StreamWriter(fs);
            sw.Write(Json_info);
            sw.Flush();
        }
        Debug.Log("資料儲存成功!!路徑:“"+ PathHelper.AppHotfixResPath+ "”");
        UnityEditor.AssetDatabase.Refresh();
    }