C#streamwriter,streamreader class (primarily for text file access)

Source: Internet
Author: User

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)

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.