I have a text file with about 3000 lines. Some lines start with numbers and some with text. For example:
Lines that start with numbers:
001 some text
0017 some text
8 some text...
Lines that start with text:
some text,
some text...
I want to copy lines that start with numbers to result.text.
Lines that start with text to log.txt.
Thanks a lot.
Okay here is how i would do it
this is enough of a hint incase this is your homework. do your research!
It can be done in one statement for one file
Note that it works on a line-by-line basis internally, i.e. there will be only one line at a time in memory. One line will be read from input processed and written to the output, then the next line and so on. This is because these methods work with
IEnumerable<string>
.IEnumerable
does not buffer the whole file as would be the case if you read the file into an array, for instance.You would have to repeat this twice for the two output files. Therefore I suggest the following approach, which reads the input file only once:
This second approach does not buffer the file either.