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);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器