unity3d学习笔记 对文件得读取与写入

本文介绍了一种使用C#进行文件夹内所有txt文件的读取方法,并实现了一个用于存储更新后的文件的具体示例。该过程涉及遍历指定目录下的所有.txt文件,读取内容并将其存储在内存中;同时,还展示了如何将JSON格式的信息写回到新的.txt文件中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//读取文件夹内所有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();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值