日期:2014-05-17 浏览次数:20987 次
谁给个详细的介绍Streamwriter和Steeamreader用法的资料或者链接,百度谷歌都用了,好多都看不懂,越详细越好!谢谢了
//获取:\test.txt的写入流,参数true说明在文本后面追加,false为每次都覆盖
StreamWriter sw = new StreamWriter(@"C:\test.txt", true);
sw.Write("向文件中写入文本。。。");
sw.WriteLine("向文件中写入一行文本。。。");
sw.Close();//关闭流
//获取C:\test.txt的读取流
StreamReader sr = new StreamReader(@"C:\test.txt");
string str = sr.ReadLine();//读取一行文本
string allTxt = sr.ReadToEnd();//读取所有文本
sr.Close();//关闭流
------解决方案--------------------
读取文本的最后一行的信息,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string line = null;
FileStream fs = new FileStream(@"D:\test.txt", FileMode.Open);
StreamReader sr = new StreamReader(fs);
while(sr.Peek()!= -1)
{
line = sr.ReadLine();
}
Console.WriteLine(line);
sr.Close();
Console.ReadKey();
}
}
}