txt文本文档的创建和读取

来源:互联网 发布:仿淘宝彩票合买源码 编辑:程序博客网 时间:2024/04/28 07:58

创建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;    }
0 0
原创粉丝点击