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 ();}