一个txt文件,有两行
aaaa
bbbb代码:
string s1 = @"D:\iosample\pp.txt";
using (StreamReader sr = new StreamReader(s1))
{
Console.WriteLine(sr.ReadLine());
}输出结果为aaaa
我想问的是:
ReadLine方法的返回值是流中的下一行,而下一行是bbbb,那返回值应该是bbbb啊?应该输出bbbb吧MSDN
aaaa
bbbb代码:
string s1 = @"D:\iosample\pp.txt";
using (StreamReader sr = new StreamReader(s1))
{
Console.WriteLine(sr.ReadLine());
}输出结果为aaaa
我想问的是:
ReadLine方法的返回值是流中的下一行,而下一行是bbbb,那返回值应该是bbbb啊?应该输出bbbb吧MSDN
{
Console.WriteLine(sr.ReadLine()); //第一行
Console.WriteLine(sr.ReadLine()); //第二行
}ReadLine可以理解为一行一行读取。如果一上来就返回第二行,那第一行怎么办
using (StreamReader sr = new StreamReader(path))
{
while (sr.Peek() >= 0)
{
Console.WriteLine(sr.ReadLine());
}
}
你贴出来的例子,你不仔细看看吗?
{ while (!sr.EndOfStream)
{
Console.WriteLine(sr.ReadLine());
}
}
string sLine=string.Empty;
using (StreamReader sr = new StreamReader(path))//遍历数据
{
while (!sr.EndOfStream)//不是末尾数据
{
sLine = sr.ReadLine();
if (sLine.Length < 1)
{
continue;
}
}
}
textBox1.Text = sLine.ToString();
最后得到的是
bbbb
文件内容
EOF:结尾开头、结尾没有任何行,可以认为阅读器初始时指向BOF。
Before of File,在电脑的术语缩写通常为 BOF,在作业系统决定资料源无更多的资料可读取。资料源通常称为档案或串流。 bof 用于指在第一条记录的前面,当记录集为空是,bof和eof都为true。