C # textreader/textwriter class

Source: Internet
Author: User

Textreader and textwriter are very useful. Many Methods accept them as parameters.

Textreader has two subclasses:

  1. Stringreader/stringwriter is used to read strings;
  2. Streamreader/streamwriter is used to read streams;
I. Role of stringreader

Stringreader is mainly used to read strings.

The common method is as follows:

Member Description
Close Disable stringreader
Peek Returns the next available character, but does not use it
Read Reads the next character in the input string and promotes the position of the character to one character. This overload can be used to increase any character.
Readasync The specified maximum number of characters to read starts from the current string asynchronous and Data Writing buffer, starting from the specified index
Readblock Reads a specified number of characters from the current text reader and writes the data to the buffer starting from the specified index.
Readblockasync The specified maximum number of characters to read starts from the current string asynchronous and Data Writing buffer, starting from the specified index
Readline Reads a line of characters from the current string and returns the data as a string.
Readlineasync Reads a line of characters asynchronously from the current string and returns the data as a string
Readtoend Read any character at the current position. End of the string and return it as a string
Readtoendasync Reads all characters at the current position as the end of the string asynchronously and returns it as a string

 

Example:

Static void main (string [] ARGs) {textreader TR = new stringreader ("are you good at home? \ N is good! "); String line1 = tr. Readline (); // read a line of console. writeline (line1); // are you good at home? String line2 = tr. Readline (); console. writeline (line2); // very good! // Note that when the pointer is reached, comment out the above four rows to see the effect. // PEEK () is read, but no, it does not affect the pointer while (TR. peek ()> 0) {console. writeline (char) tr. read (); // read all characters one by one} Char [] chararr = new char [4]; tr. readblock (chararr, 1, 3); For (INT I = 0; I <chararr. count (); I ++) {console. writeline (chararr [I]);} console. readkey ();}
Ii. Use of streamreader

Streamreader attributes:

Attribute Description
Basestream Back to base stream
Currentencoding Obtains the encoding of the current character used by the current streamreader object.
Endofstream Obtains a value indicating whether the current stream position is at the end of the stream.

Streamreader method:

Method Description
Close Close streamreader objects and basic streams, and release all system resources associated with the reader.
Discardbuffereddata Clear internal buffer
Dispose Release all resources used by the textreader object
Peek Returns the next available character, but does not use it
Read Read the next character in the input stream and increase the character position by one character.
Readasync Asynchronously reads a specified number of characters from the current stream and writes the data to the buffer starting from the specified index.
Readblock Reads a specified number of characters from the current stream and writes the data to the buffer zone from the specified index.
Readblockasync Asynchronously reads a specified number of characters from the current stream and writes the data to the buffer starting from the specified index.
Readline Reads a line of characters from the current stream and returns the data as a string.
Readlineasync Asynchronously reads a line of characters from the current stream and returns the data as a string.
Readtoend Reads all characters from the current position of the stream to the end
Readtoendasync Asynchronously reads all characters from the current position to the end of the stream and returns them as a string

Example:

        static void Main(string[] args)        {            FileStream fs = new FileStream(@"D:\123.txt",FileMode.Open,FileAccess.Read);            TextReader tr = new StreamReader(fs,Encoding.Default);            string str = tr.ReadToEnd();            Console.WriteLine(str);            Console.ReadKey();        }

 

Iii. Role of stringwriter

Stringwirter is mainly used to read and write strings.

Common attributes:

Attribute Description
Encoding Obtain the encoding in which the output is written.
Formatprovider Get the object of control format settings
Newline Obtains or sets the row Terminator string used by the current textwriter.

Common Methods:

Method Description
Close Close the current stringwriter and basic stream
Dispose Release stringwriter Resources
Flush Clears all the buffers of the current writer so that all the buffered data is written to the basic device.
Getstringbuilder Return to basic stringbuilder
Write Written to this instance of stringwriter
Writeline Write some data specified by the overload parameter, followed by the row Terminator

Example:

Static void main (string [] ARGs) {stringbuilder sb = new stringbuilder (); sb. append ("are you good at home? "); Using (textwriter Tw = new stringwriter (SB) {TW. writeline (" difficult to mix! "); Console. writeline (sb. tostring (); // output: Are you in another place? Difficult to mix! (There is a line break next to it, but not write)} console. readkey ();}

 

Iv. Functions of streamwriter

Streamwriter writes a piece of content to a stream, including filestream and memorystream.

Attribute:

Attribute Description
Autoflush Gets or sets a value indicating whether streamwriter refresh the buffer to the base stream after streamwriter. Write is called each time.
Basestream Obtain the basic stream connected to the same backup storage area
Encoding Obtain the encoding in which the output is written.

 

Method:

Method Description
Close Close the current streamwriter object and basic stream
Flush Clears all the buffers of the current writer and writes all the buffered data to the base stream.
Flushasync Asynchronously clears all buffers of the stream and causes all buffered data to be written to the base device.
Write Write characters to the stream
Writeasync Write characters to the stream Asynchronously
Writeline Write the character to the stream and add the Terminator.
Writelineasync Asynchronously writes the row terminator to the stream.

 

Example:

Static void main (string [] ARGs) {using (streamwriter Tw = new streamwriter (@ "D: \ 123.txt") {TW. writeline (" difficult to mix! "); Console. writeline (TW. basestream. GetType (); // output filestream} console. readkey ();}

C # textreader/textwriter class

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.