C # query txt text (code example ),

Source: Internet
Author: User

C # query txt text (code example ),
C # query txt text (code example)

''' // Introduce the namespace using System. IO; using System. text; // method 1 public static void txtReading2 () {// open a Text File, read all rows, and close this document string str1 = File. readAllText (@ "D: \ flie.txt"); // you can specify the encoding method string str2 = File. readAllText (@ "D: \ flie.txt", Encoding. ASCII); // method 2 // usage method File. readAllLines, the same as ReadAllText. This method reads all rows of text content at a time and returns a string array. The array element is the content of each row, string [] strs1 = File. readAllLines (@ "D: \ flie.txt"); // specify the encoding method string [] strs2 = File. readAllLines (@ "D: \ flie.txt", Encoding. default); // read the data in each row in turn. foreach (string s in strs2) {Console. writeLine (s) ;}}// method 2, byte stream read public static void txtReading () {// configurable access permissions // FileStream fS = new FileStream (@ "D: \ flie.txt ", FileMode. open, FileAccess. read, FileShare. none); // StreamRe Ader sR4 = new StreamReader (fS, Encoding. UTF8); // FileInfo provides the function of adding, deleting, modifying, and querying the price, and enabling FileInfo myFile = new FileInfo (@ "D: \ flie.txt "); // OpenText creates an UTF-8-encoded StreamReader object to read the myFile file StreamReader sR5 = myFile. openText (); // It can also be written in this way. Create a StreamReader object to open an existing UTF-8 file to read. // StreamReader sR5 = File. openText (@ "D: \ flie.txt"); // read the first line of the character from the current stream and return it as a string. string nextLine = sR5.ReadLine (); // read all, reading from the current position of the stream to the end is followed by the upstream code, and the current position is the second line starting string restOfStream = sR5.ReadToEnd (); // loop the remaining stream from the current position of the stream. Is to continue the upstream code. The current position is string nextLines starting from the third line; while (nextLines = sR5.ReadLine ())! = Null) {Console. WriteLine (nextLine) ;}// close the sR5.Close () object ();}

Related Article

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.