展开全部
int lineno = 1; //删除行号
int highByte = 0; //判断换行符ASCII-13
int lowByte = 0; //判断换行符ASCII-10
long readCount = 0; //读取字节数
long startCount = 0; //删除行号起始字节数
long fileLength = 0; //文件长度
List<byte> buffer = new List<byte>();
using (FileStream fs = new FileStream("C:\\1.txt", FileMode.Open, FileAccess.ReadWrite))
{
fileLength = fs.Length;
while (lineno > -1) //扫描需要删除的行号
{
lowByte = fs.ReadByte();
if (highByte == 13 && lowByte == 10)
{
highByte = 0;
lineno--; //若遇到换行符则表示已经读取一行
if (lineno == 0)
startCount = readCount + 1;
}
if (lowByte == 13)
{
highByte = 13;
}
readCount++;
}
for (long i = readCount; i < fileLength; i++) //读取删除之后的文件内容
{
buffer.Add((byte)fs.ReadByte());
}
fileLength = fileLength - (readCount - startCount);
fs.SetLength(fileLength); //减小文件长度用于删除内容
fs.Position = startCount--;
byte[] write = buffer.ToArray();
fs.Write(write, 0, write.Length); //降删除行后的内容提前,即删除行
}
int highByte = 0; //判断换行符ASCII-13
int lowByte = 0; //判断换行符ASCII-10
long readCount = 0; //读取字节数
long startCount = 0; //删除行号起始字节数
long fileLength = 0; //文件长度
List<byte> buffer = new List<byte>();
using (FileStream fs = new FileStream("C:\\1.txt", FileMode.Open, FileAccess.ReadWrite))
{
fileLength = fs.Length;
while (lineno > -1) //扫描需要删除的行号
{
lowByte = fs.ReadByte();
if (highByte == 13 && lowByte == 10)
{
highByte = 0;
lineno--; //若遇到换行符则表示已经读取一行
if (lineno == 0)
startCount = readCount + 1;
}
if (lowByte == 13)
{
highByte = 13;
}
readCount++;
}
for (long i = readCount; i < fileLength; i++) //读取删除之后的文件内容
{
buffer.Add((byte)fs.ReadByte());
}
fileLength = fileLength - (readCount - startCount);
fs.SetLength(fileLength); //减小文件长度用于删除内容
fs.Position = startCount--;
byte[] write = buffer.ToArray();
fs.Write(write, 0, write.Length); //降删除行后的内容提前,即删除行
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询