The ReadLine method in the C # console base StreamReader reads all the lines of the specified TXT file

Source: Internet
Author: User

1. Code

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.IO;4 usingSystem.Linq;5 usingSystem.Text;6 usingSystem.Threading.Tasks;7 8 namespaceConsoleApplication49 {Ten     class Program One     { A         Static voidMain (string[] args) -         { -             using(StreamReader Sreader =NewStreamReader (@"1.txt", Encoding.default)) the             { -                 stringALine; -                 //controls whether a while loop makes a variable, true print text, false jumps out of the loop -                 BOOLCondition =true; +  -                  while(true) +                 { A                      atALine =sreader.readline (); -  -                     //Aline=null launched the text read, then the control amount of condition combined if statement action is to jump out of the loop -                     //if the text is not finished reading, then the function of the condition union if statement is to output the read text -                     if(aline==NULL) -                     { inCondition =false; -                     } to                      +                     if(condition) -                     { the Console.WriteLine (aLine); *                     } $                     ElsePanax Notoginseng                     { -                          Break; the                     } +                 } A  the                  +             } - Console.readkey (); $         } $     } -}

2. txt file

3. Effect

You can also make improvements to the code, using the members that the class comes with

1. Code

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.IO;4 usingSystem.Linq;5 usingSystem.Text;6 usingSystem.Threading.Tasks;7 8 namespaceConsoleApplication49 {Ten     class Program One     { A         Static voidMain (string[] args) -         { -             using(StreamReader Sreader =NewStreamReader (@"1.txt", Encoding.default)) the             { -                 //if not to the end -                   while(!Sreader.endofstream) -                 { + Console.WriteLine (Sreader.readline ()); -                 } +             } A Console.readkey (); at         } -     } -}

2. Effect

The ReadLine method in the C # console base StreamReader reads all the lines of the specified TXT file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.