一、 定义路径
string filePath = Application.dataPath + "/Resources/"+ _jsonName;
二、创建
StreamWriter sw;
FileInfo file = new FileInfo(filePath);
sw = file.CreateText();
三、写入
StreamWriter sw;
FileInfo file = new FileInfo(filePath);
sw = file.AppendText();
sw.WriteLine("要添加的文本");
sw.Close();
sw.Dispose();
四、读取
StreamReader sr;
try
{
sr = File.OpenText(filePath);
string strData = sr.ReadToEnd();
sr.Close();
}
catch
{
return null;
}
五、清空内容
方法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();