
[Unity][Android][LUA][IOS]读取写入txt文件
发布日期:2021-05-09 11:52:45
浏览次数:20
分类:精选文章
本文共 2074 字,大约阅读时间需要 6 分钟。
接着上一篇继续
创建 txt 文件,来保存文件内容。读取和写入。
从test文件 中读取内容,并创建 新的 文件test1.txt,替换 test1.txt 的某些内容。
显示结果
test1文件(test1.txt文件内容如果 不清空,就会 在原有文件中继续 写入 内容,根据情况,自行判断。)
using System.Collections;using System.Collections.Generic;using UnityEngine;using SLua;using System.IO;public class Test : MonoBehaviour { public TextAsset TxtFile; //建立TextAsset private string Mytxt; //用来存放文本内容 LuaSvr l; private string m_sFileName = "test1.txt"; // 文件名 // Use this for initialization void Start() { l = new LuaSvr(); l.init(null, () => { l.start("test"); }); string path = ""; if (Application.platform == RuntimePlatform.Android) { path = Application.persistentDataPath; } else if (Application.platform == RuntimePlatform.WindowsPlayer) { path = Application.dataPath; } else if (Application.platform == RuntimePlatform.WindowsEditor) { path = Application.dataPath; } Mytxt = TxtFile.text; print(Mytxt); //输出验证 //找到文件 替换 某些内容 string text = Mytxt.Replace("Hello","Well Done!123"); CreateOrOPenFile(path, m_sFileName, text); } void CreateOrOPenFile(string path, string name, string info) { //路径、文件名、写入内容 FileInfo fi = new FileInfo(path + "//" + name); StreamWriter sw ; // StreamWriter sw = new StreamWriter(path+ name, false); //File.Create(path + name); //sw.WriteLine(""); print(" create"); //删除文件,重写文件的内容 fi.Delete(); if (!fi.Exists) { //File.Create(path+name); //创建文件 sw = fi.CreateText(); } else { sw = fi.AppendText(); } //在 文件原有内容上 ,写入文件 sw.WriteLine(info); sw.Close(); sw.Dispose(); }}//在这里 把 路径 加上 /Slua/Resources/就效果如下所示 //找到文件 替换 某些内容 string text = Mytxt.Replace("Hello","Well Done!123"); CreateOrOPenFile(path+"/Slua/Resources/", m_sFileName, text);
参考资料:
1.
2.
3.
4.
5.
6.
发表评论
最新留言
第一次来,支持一个
[***.219.124.196]2025年05月07日 23时23分46秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
MySQL错误日志(Error Log)
2019-03-06
解决:angularjs radio默认选中失效问题
2019-03-06
windows环境下安装zookeeper(仅本地使用)
2019-03-06
缓冲区溢出实例(一)--Windows
2019-03-06
Hadoop学习笔记—Yarn
2019-03-06
Jenkins - 部署在Tomcat容器里的Jenkins,提示“反向代理设置有误”
2019-03-06
wxWidgets源码分析(3) - 消息映射表
2019-03-06
wxWidgets源码分析(5) - 窗口管理
2019-03-06
wxWidgets源码分析(8) - MVC架构
2019-03-06
wxWidgets源码分析(9) - wxString
2019-03-06
[梁山好汉说IT] 梁山好汉和抢劫银行
2019-03-06
[源码解析] 消息队列 Kombu 之 基本架构
2019-03-06
[源码分析] 消息队列 Kombu 之 启动过程
2019-03-06
wx.NET CLI wrapper for wxWidgets
2019-03-06
ASP.NET MVC Action Filters
2019-03-06
Powershell中禁止执行脚本解决办法
2019-03-06
OO_Unit2 多线程电梯总结
2019-03-06
04_Mysql配置文件(重要参数)
2019-03-06