【C#】读取和写入本地txt文件
本次我们将使用 C# 实现本地 txt 文件的读取和写入操作,该方法在 Unity 开发过程中同样适用。下面将详细介绍具体的实现步骤。
1. 创建文件操作类 MyFileStream
我们需要创建一个用于文件打开、关闭、读取和写入的类 MyFileStream。在这个类中,需要引入 System.IO 和 System.Runtime.Serialization.Formatters.Binary 命名空间,前者用于文件读取操作,后者为二进制类。以下是具体代码:
using UnityEngine;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class MyFileStream
{
FileStream file;
StreamReader st;
StreamWriter sw;
public bool Open(string FileName, FileAccess Mode)
{
bool Success = false;
string path = PathForDocumentsFile(FileName);
if (Mode == FileAccess.Read)
{
if (File.Exists(path))
{
file = new FileStream(path, FileMode.Open, FileAccess.Read);
if (file != null)
{
st = new StreamReader(file);
Success = true;
}
}
}
else if (Mode == FileAccess.Write)
{
file = new FileStream(path, FileMode.Create, FileAccess.Write);
if (file != null)
{
sw = new StreamWriter(file);
Success = true;
}
}
return Success;
}
public string ReadLine()
{
return st.ReadLine();
}
public void WriteLine(string Line)
{
sw.WriteLine(Line);
}
public void Close()
{
if (st != null)
{
st.Close();
st = null;
}
if (sw != null)
{
sw.Close();
sw = null;
}
if (file != null)
{
file.Close();
file = null;
}
}
public string PathForDocumentsFile(string filename)
{
// 暂时只判断安卓和 PC
if (Application.platform == RuntimePlatform.Android)
{
string path = Application.persistentDataPath;
path = path.Substring(0, path.LastIndexOf('/'));
return Path.Combine(path, filename);
}
else
{
string path = Application.dataPath;
path = path.Substring(0, path.LastIndexOf('/'));
return Path.Combine(path, filename);
}
}
}
代码解释:
Open方法:根据传入的文件名和文件访问模式(读取或写入)来打开文件。如果是读取模式,会先检查文件是否存在;如果是写入模式,则会创建新文件。ReadLine方法:用于从文件中读取一行内容。WriteLine方法:用于向文件中写入一行内容。Close方法:关闭文件流、读取器和写入器,并将其置为null,释放资源。PathForDocumentsFile方法:根据不同的平台(安卓或 PC)生成文件的完整路径。
2. 创建数据管理类 MyLocalData
接下来,我们创建一个用于管理需要保存的数据和调用文件操作的类 MyLocalData。以下是具体代码:
using UnityEngine;
using System.Collections;
public class MyLocalData : MonoBehaviour
{
private static MyLocalData _instance;
public static MyLocalData Instance()
{
if (_instance == null)
{
_instance = new MyLocalData();
}
return _instance;
}
public class MyLocalSaveData
{
public int test01;
public int test02;
public int test03;
}
public MyLocalSaveData m_Data = new MyLocalSaveData();
MyFileStream m_FS = new MyFileStream();
string m_FileName = "LocalSaveData.txt";
public void InitLoad()
{
if (m_FS.Open(m_FileName, System.IO.FileAccess.Read))
{
m_Data.test01 = 0;
m_Data.test02 = 0;
m_Data.test03 = 0;
int Num = 0;
if (int.TryParse(m_FS.ReadLine(), out Num))
{
m_Data.test01 = Num;
}
if (int.TryParse(m_FS.ReadLine(), out Num))
{
m_Data.test02 = Num;
}
if (int.TryParse(m_FS.ReadLine(), out Num))
{
m_Data.test03 = Num;
}
m_FS.Close();
}
}
public void Write()
{
if (m_FS.Open(m_FileName, System.IO.FileAccess.Write))
{
m_FS.WriteLine(m_Data.test01.ToString());
m_FS.WriteLine(m_Data.test02.ToString());
m_FS.WriteLine(m_Data.test03.ToString());
m_FS.Close();
}
}
}
代码解释:
Instance方法:实现单例模式,确保MyLocalData类只有一个实例。MyLocalSaveData类:定义了需要保存的数据字段。InitLoad方法:从文件中读取数据并赋值给m_Data对象的相应字段。Write方法:将m_Data对象的字段值写入文件。
3. 调用示例
可以将 MyLocalData 类挂载在场景中的一个 GameObject 上,然后进行读取和写入操作。示例代码如下:
// 读取操作
int value = MyLocalData.Instance().m_Data.test01;
// 写入操作
MyLocalData.Instance().m_Data.test01 = 1;
MyLocalData.Instance().Write();
版权信息
Ricky Yang 个人原创,版权所有,转载注明,谢谢。个人博客
泰斗原文:http://www.taidous.com/thread-32688-1-1.html?_dsign=7fad4c27