The File.ReadAllLines () method is first used to read the contents of the text document and return the string array contents, so that each row of data becomes an element of the string array contents, and then uses the split () method to separate each element with a space or tab, Returns a string array so that a row of data is returned with 3 strings into the string array, converted to a double type by the Convert.todouble () method, and evaluated. Create a StringBuilder data type to add a string of each row, convert to string, and then write the newly created document with the File.writealltext method.
The advantage is simple, the disadvantage is that only small documents can be read into
, the Space or tab spacing is OK
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.IO;6 usingSystem.Diagnostics;//Stopwatch name Space7 8 namespaceTotal book price calculation9 {Ten class Program One { A Static voidMain (string[] args) - { - stringPath =@"C:\Users\Administrator\Desktop\ Title Total price calculation of. txt"; the string[] contents = file.readalllines (path, encoding.default);//put all the contents of a document into a string array - string[] strnew; - stringstrresult; - +Stopwatch SW =NewStopwatch ();//Create a Timer method -Sw. Start ();//Start Timing +StringBuilder sb=NewStringBuilder ();//Create SB A at for(inti =0; I < contents. Length; i++)//start with the second line - { - if(I! =0) - { -Strnew = Contents[i]. Split (New Char[] {' ','\ t'}, stringsplitoptions.removeemptyentries); -Console.WriteLine ("{0} {1} {2} {3}", strnew[0], strnew[1], strnew[2], convert.todouble (strnew[1]) * Convert.todouble (strnew[2])); inSb. Append (strnew[0]. ToString () +" "+ strnew[1]. ToString () +" "+ strnew[2]. ToString () +" "+ (Convert.todouble (strnew[1]) * Convert.todouble (strnew[2])). ToString () +System.Environment.NewLine); - } to Else//The first row of headers does not participate in calculating the total price + { -Strnew = Contents[i]. Split (New Char[] {' ','\ t'}, stringsplitoptions.removeemptyentries); the //combine character arrays into a single string * //strresult=string. Join ("", new string[] {strnew[0], strnew[1], strnew[2]}); $ //strresult = string. Join ("", strnew);Panax Notoginseng //Console.WriteLine (strresult); -Console.WriteLine ("{0} {1} {2} Total price", strnew[0], strnew[1], strnew[2]); theSb. Append (strnew[0]. ToString () +" "+ strnew[1]. ToString () +" "+ strnew[2]. ToString () +"Total Price"+System.Environment.NewLine); + } A } the Console.WriteLine (SB); + //output SB's string to a new text document -File.writealltext (@"C:\Users\Administrator\Desktop\ Title Total price calculation results. txt", sb. ToString ()); $Sw. Stop ();//end timing, output in milliseconds $Console.WriteLine (SW. Elapsedmilliseconds);//output results in milliseconds - } - } the}
Reference: http://blog.sina.com.cn/s/articlelist_2379298071_7_1.html
C # Read text document Practice 3-writing to text this document