1. Why use StreamReader or StreamWriter
If it is necessary to read a portion of the text file to display a portion of it, then using FileStream will be problematic, since it is possible that FileStream will separate the bytes of a Chinese character at the time of reading. So the string is not displayed correctly when the display is displayed. So the StreamReader class is generally used for reading large text files. For large text files write generally with the StreamWriter class.
2, StreamWriter
1. Create a StreamWriter using (StreamWriter SW = new StreamWriter ("Test.txt", False, Encoding.UTF8)) { //2. Perform read -write for (int i = 0; i < i++) { sw. WriteLine (i + "======" + System.DateTime.Now.ToString ()); } } Console.WriteLine ("OK"); Console.readkey ();
3, StreamReader
#region StreamReader use//1. Create StreamReader object using (StreamReader reader = new Streamread ER ("English-Chinese Dictionary txt format. txt", Encoding.default)) {#region 1 while (!reader). Endofstream) {String line = reader. ReadLine (); Console.WriteLine (line); } #endregion #region 2 int count = 0; while (reader. ReadLine ()! = null) {count++; Console.WriteLine (reader. ReadLine ()); } Console.WriteLine (count); File.ReadAllLines (#endregion #region 2//2. Iterates through each row of data. string line = null; int count = 0; while (line = reader. ReadLine ()) = null) {count++; Console.WriteLine (line); }//console.writeline (count); #endregion} Console.WriteLine ("OK"); Console.readkey (); #endregion
C#streamwriter,streamreader class (primarily for text file access)