I have a text file containing 100 lines. I want to delete first 20 lines from that file I have a following code
IS anyone tell me how to do that? this code only deleting 1 line. I want to delete first 20 lines
string line = null;
int line_number = 0;
int line_to_delete = 12;
using (StreamReader reader = new StreamReader("C:\\input")) {
using (StreamWriter writer = new StreamWriter("C:\\output")) {
while ((line = reader.ReadLine()) != null) {
line_number++;
if (line_number == line_to_delete)
continue;
writer.WriteLine(line);
}
}
}
How about using some Linq
and static
methods? If performance is not crucial(the code will read all lines into memory):
File.WriteAllLines("outputfile",
File.ReadLines("inputfile").Skip(20));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.