C # understanding/using streamreader, streamwriter, stringreader, and stringwriter

Source: Internet
Author: User

URL: http://www.cnblogs.com/zhxhdean/archive/2012/02/20/2359605.html

Question

Streamreader, streamwriter, stringreader, and stringwriter can be confused (not deeply understood) every time they see these words. They are often confused during use. To better understand these words, write thisArticle.

Body Concept

First, we can see from the literal that streamreader and stringreader are used for read operations, streamwriter and stringwriter are used for write operations.

Next, let's look at the layers of these classes in the class library.

Both streamreader and stringreader are derived from the textreader class (Textreader: Indicates that consecutive character sequences can be read.Reader. Abstract base class)

Streamreader reads characters from the stream

Stringreader reads characters from strings

, Streamwriter, stringwriter derived from textwriter class (Textwriter: Indicates that an ordered character sequence can be compiled.Writer. This class is an abstract class .)

Streamwriter writes characters to the stream

Stringwriter writes characters to strings

Code

Reader operation

Private Static void stringread ()
{
String in_str = "stream operations, character operations, Io operations ";
String read_str, aparagraph = NULL;
Stringreader sr = new stringreader (in_str );
While (true)
{
Read_str = Sr. Readline ();
If (read_str! = NULL)
{
Aparagraph = aparagraph + in_str + "";
}
Else
{
Aparagraph = aparagraph + "\ n ";
Break;
}
}
}

Private Static void streamread ()
{
Stream rstream = new filestream (@ "E:/20120214.txt", filemode. Open );
Streamreader sr = new streamreader (rstream, encoding. utf8 );
// Read characters from the stream
String result = Sr. readtoend ();
// Release resources
Sr. Close ();
Rstream. Close ();
}

 

Writer operation

Private Static void streamwriter ()
{
String in_str = datetime. Now. tostring ("mm-dd hh: mm: SS: fff ");
// Whether the second parameter is appended
Streamwriter Sw = new streamwriter (@ "E:/20120214.txt", true, encoding. Default );
Sw. Write (in_str );
Sw. Flush ();
Sw. Close ();
}

Private Static void stringwriter ()
{
Stringwriter Sw = new stringwriter ();
Sw. Write ("text, learning, text, C #");
Sw. Close ();
}

Category: C # question

Streamreader, streamwriter, stringreader, and stringwriter can be confused (not deeply understood) every time they see these words. They are often confused during use. In order to better understand these words, write down this article.

Body Concept

First, we can see from the literal that streamreader and stringreader are used for read operations, streamwriter and stringwriter are used for write operations.

Next, let's look at the layers of these classes in the class library.

Both streamreader and stringreader are derived from the textreader class (Textreader: Indicates that consecutive character sequences can be read.Reader. Abstract base class)

Streamreader reads characters from the stream

Stringreader reads characters from strings

, Streamwriter, stringwriter derived from textwriter class (Textwriter: Indicates that an ordered character sequence can be compiled.Writer. This class is an abstract class .)

Streamwriter writes characters to the stream

Stringwriter writes characters to strings

Code

Reader operation

Private Static void stringread ()
{
String in_str = "stream operations, character operations, Io operations ";
String read_str, aparagraph = NULL;
Stringreader sr = new stringreader (in_str );
While (true)
{
Read_str = Sr. Readline ();
If (read_str! = NULL)
{
Aparagraph = aparagraph + in_str + "";
}
Else
{
Aparagraph = aparagraph + "\ n ";
Break;
}
}
}

Private Static void streamread ()
{
Stream rstream = new filestream (@ "E:/20120214.txt", filemode. Open );
Streamreader sr = new streamreader (rstream, encoding. utf8 );
// Read characters from the stream
String result = Sr. readtoend ();
// Release resources
Sr. Close ();
Rstream. Close ();
}

 

Writer operation

Private Static void streamwriter ()
{
String in_str = datetime. Now. tostring ("mm-dd hh: mm: SS: fff ");
// Whether the second parameter is appended
Streamwriter Sw = new streamwriter (@ "E:/20120214.txt", true, encoding. Default );
Sw. Write (in_str );
Sw. Flush ();
Sw. Close ();
}

Private Static void stringwriter ()
{
Stringwriter Sw = new stringwriter ();
Sw. Write ("text, learning, text, C #");
Sw. Close ();
}

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.