简体   繁体   English

StreamReader读取文件,StreamWriter写入结果,省略第一行吗?

[英]StreamReader read file and StreamWriter write that result omitting first line?

Using the following code: 使用以下代码:

string lines = "";
using (StreamReader sr = new StreamReader(@"file.txt"))
{
    lines = sr.ReadLine();
}
using (StreamWriter writer = new StreamWriter(@"file.txt"))
{
    writer.Write(lines); // Change this to skip the first line
}

How can I make it rewrite everything EXCEPT the first line? 如何使它重写第一行以外的所有内容?

var allLinesExceptFirstOne = System.IO.File.ReadAllLines(filename).Skip(1).ToList();

Maybe you can try this: 也许您可以尝试以下方法:

var lines =  File.ReadLines("file.txt").Skip(1).ToList();
File.WriteAllLines("file.txt",lines);

It will write all the lines to your file except the first line and replace your file content.So basically it will remove the first line from your file. 它将把除第一行以外的所有行都写入文件,并替换文件内容。因此,基本上它将从文件中删除第一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想通过 StreamReader 从文本文件中读取数据,编辑特定字符串并将其写入文件中 - I want to read data from a text file via StreamReader, edit a specific string and write it to the file vis StreamWriter 更新文件文本流读取器或流写入器 - Updating the file text streamreader or streamwriter 如何从 streamreader 读取第一行的第一个字符? (例如 txt 文件中的字母 H - How can I read from streamreader the first character from the first line? ( Example the Letter H that is in the txt file 使用streamreader / streamwriter在C#中逐行编写文件非常慢 - Writing file line by line in C# very slow using streamreader/streamwriter StreamReader和Streamwriter - StreamReader and streamwriter 使streamreader读取文本文件中的特定行,然后写入控制台C# - Getting streamreader to read specific line in text file, then write to console c# 使用StreamReader从文件读取数据到第1行的数组中 - read data from file with StreamReader into array of line 1 C#只读取第一行,使用压缩文本文件的StreamReader - C# Read only first line, using StreamReader of a zipped text file 如何使用 StreamReader 和 StreamWriter 创建文件副本 - How to create copy of file using StreamReader and StreamWriter C#streamreader和streamwriter到同一文件 - C# streamreader and streamwriter onto same file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM