1.添加命名空间
System.IO;
System.Text;
2.文件的读取
#region 读取TXT文本文件 /// <summary> /// FileStream读取文本文件 /// </summary> public void FileStreamRead() { //文件路径 string filePath = AppDomain.CurrentDomain.BaseDirectory; // Server.MapPath("~/UploadFiles/"); //文件夹不存在则创建 if (!System.IO.Directory.Exists(filePath)) { System.IO.Directory.CreateDirectory(filePath); } filePath = AppDomain.CurrentDomain.BaseDirectory + DateTime.Today.ToString("yyyMMdd") + ".txt"; if (System.IO.File.Exists(filePath)) { //1.直接读取出字符串 string strText = System.IO.File.ReadAllText(filePath); //2.按行读取为字符串数组 string[] arrFileText = System.IO.File.ReadAllLines(filePath); //3.FileStream读取写入给定的缓存区 System.IO.FileStream fs = new FileStream(filePath, FileMode.Open); fs.Seek(0, SeekOrigin.Begin); byte[] byData = new byte[100]; fs.Read(byData, 0, 100);//byData传进来的字节数组,用以接受FileStream对象中的数据 System.Text.Decoder d = System.Text.Encoding.Default.GetDecoder(); char[] charData = new char[1000]; d.GetChars(byData, 0, byData.Length, charData, 0); fs.Close(); } } /// <summary> /// StreamReader读取文本文件 /// </summary> public void StreamReaderRead() { //文件路径 string filePath = AppDomain.CurrentDomain.BaseDirectory; // Server.MapPath("~/UploadFiles/"); //文件夹不存在则创建 if (!System.IO.Directory.Exists(filePath)) { System.IO.Directory.CreateDirectory(filePath); } filePath = AppDomain.CurrentDomain.BaseDirectory + DateTime.Today.ToString("yyyMMdd") + ".txt"; if (System.IO.File.Exists(filePath)) { //1.从头到尾以流的方式读出文本文件,该方法会读出一行文本 System.IO.StreamReader sr = new StreamReader(filePath); string strStreamReader = sr.ReadToEnd(); sr.Close(); } } #endregion
FileStream fs = new FileStream(@"c: empascii.txt", FileMode.Open, FileAccess.Read, FileShare.Read); //StreamReader sr3 = new StreamReader(fs); //string str3 = sr3.ReadToEnd(); //sr3.Close(); StreamReader sr4 = new StreamReader(fs, System.Text.Encoding.Default); string strCH = sr4.ReadToEnd(); sr4.Close(); string str2 = System.IO.File.ReadAllText(@"c: empascii.txt", System.Text.Encoding.ASCII);
3.文件的写入
#region 写入TXT文本文件 /// <summary> /// StreamWriter写入文本文件 /// </summary> public void StreamWriterWrite() { //文件路径 string filePath = AppDomain.CurrentDomain.BaseDirectory; // Server.MapPath("~/UploadFiles/"); //文件不存在则创建 if (!System.IO.Directory.Exists(filePath)) { System.IO.Directory.CreateDirectory(filePath); } filePath = AppDomain.CurrentDomain.BaseDirectory + DateTime.Today.ToString("yyyMMdd") + ".txt"; #region 另一种方式 ////FileMode.Append,FileAccess.Write追加文件 //FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate); //StreamWriter sw = new StreamWriter(fs); #endregion StreamWriter sw = new StreamWriter(filePath, true); //Write直接追加文件末尾,不换行;WriteLine直接追加文件末尾,换行 sw.WriteLine("测试StreamWriter写入TXT文件" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff")); //清空缓冲区、关闭流 sw.Flush(); sw.Close(); //直接追加到文件 //using (System.IO.StreamWriter sw = System.IO.File.AppendText(filePath)) //{ // sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff")); //} } /// <summary> /// FileStream写入文本文件 /// </summary> public void FileStreamWrite() { //文件路径 string filePath = AppDomain.CurrentDomain.BaseDirectory; // Server.MapPath("~/UploadFiles/"); //文件不存在则创建 if (!System.IO.Directory.Exists(filePath)) { System.IO.Directory.CreateDirectory(filePath); } filePath = AppDomain.CurrentDomain.BaseDirectory + DateTime.Today.ToString("yyyMMdd") + ".txt"; //1.该方法写入字符数组换行显示 string[] arrFile = { "first line", "second line", "third line", "第四行" }; System.IO.File.AppendAllLines(filePath, arrFile, System.Text.Encoding.Default); //2.字符串写入文本 string strTest = "该例子测试一个字符串写入文本文件。"; System.IO.File.AppendAllText(filePath, strTest, System.Text.Encoding.Default); //3.FileMode.Append,FileAccess.Write追加文件 FileStream fs = new FileStream(filePath, FileMode.Append,FileAccess.Write); byte[] data = System.Text.Encoding.Default.GetBytes("测试FileStream写入TXT文件" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff")); fs.Write(data, 0, data.Length); //清空缓冲区、关闭流 fs.Flush(); fs.Close(); } #endregion
#region 读取 保存 ////读取 //string strFileTxt = string.Empty; //using (FileStream fs = new FileStream(strFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) //{ // StreamReader sr = new StreamReader(fs, System.Text.Encoding.UTF8); //选择编码方式 // strFileTxt = sr.ReadToEnd(); //} ////保存 //using (FileStream fs = new FileStream(strSavePath, FileMode.Create, FileAccess.Write)) //{ // byte[] fData = Encoding.UTF8.GetBytes(strFileTxt); // fs.Write(fData, 0, fData.Length); // fs.Flush(); //} #endregion
http://www.cnblogs.com/jx270/archive/2013/04/14/3020456.html