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 ();
}