第2个回答 2009-10-17
using System.IO;
class Program
{
static void Main(string[] args)
{
string oldValue = string.Empty,newValue = string.Empty;
using (StreamReader read = new StreamReader(@"c:\\a.txt", true))
{
do
{
newValue = read.ReadLine();
oldValue = newValue != null ? newValue : oldValue;
} while (newValue != null);
}
Console.WriteLine(oldValue);//输出 ccccccccccc
}
/*
a.txt 文件内容:
aaaaaaaaaa
bbbbbbbbb
ccccccccccc
*/
}本回答被提问者和网友采纳
第3个回答 2015-07-07
FileStream fs = new FileStream("1.txt", FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
Queue<long> last5pos = new Queue<long>();
last5pos.Enqueue(0);
fs.Seek(0, SeekOrigin.Begin);
long pos = 0;
while (!sr.EndOfStream)
{
char cur = (char)sr.Read();
pos ++;
if(cur == '\r')
{
char next = (char)sr.Peek();
if (next == '\n')
{
last5pos.Enqueue(pos);
if (last5pos.Count > 5)
last5pos.Dequeue();
}
}
}
sr.Close();
long[] poslist = last5pos.ToArray();
for(int i=0; i< poslist.Length; i++)
Console.WriteLine(poslist[i].ToString());
if(poslist[poslist.Length - 1] + 1 == fs.Length)
{
fs.Seek(poslist[poslist.Length -2] + 1, SeekOrigin.Begin);
byte[] buffer = new byte[poslist[poslist.Length - 1] - poslist[poslist.Length -2] - 1];
fs.Read(buffer, 0, buffer.Length);
Console.WriteLine(Encoding.Default.GetString(buffer));
}
else
{
fs.Seek(poslist[poslist.Length -1] + 1, SeekOrigin.Begin);
byte[] buffer = new byte[fs.Length - poslist[poslist.Length -1] - 1];
fs.Read(buffer, 0, buffer.Length);
Console.WriteLine(Encoding.Default.GetString(buffer));
}
fs.Close();