创建txt文档
判断当前文档是否存在,不存在创建这个文件,将数据写入到文件中
public void CreateTxt(string fileName,String filePath)
{
//如果不存在则创建
if (!File.Exists(filePath))
{
using (FileStream fs = File.Create(filePath))
{
fs.Flush();
fs.Close();
fs.Dispose();
}
using (StreamWriter sw = new StreamWriter(filePath, true))
{
sw.WriteLine(fileName);
sw.Flush();
sw.Close();
sw.Dispose();
}
}
//刷新编辑器
AssetDatabase.Refresh();
}
读取txt文档
使用文件流的方式进行加载txt文本
public string ReadTxt(string filePath)
{
string updateFileContent = "";
using (StreamReader sr = File.OpenText(filePath))
{
updateFileContent = sr.ReadToEnd();
}
//String.Trim() 表示去掉首尾的多余的字节数
updateFileContent = updateFileContent.Trim(new char[] { '\n' });
return updateFileContent;
}