C # Learning Notes---Distinguishing between stringwriter (reader) and StreamWriter (reader), TextWriter (reader), BinaryWriter (reader)

Source: Internet
Author: User

1.TextWriter (reader) is a writer (reader) for continuous character series processing, from System.IO

2.StringWriter (reader) inherits TextWriter (reader), which is primarily a class that handles strings, with the following examples:

TextWriter StringWriter = new StringWriter ();
StringWriter. Write ("Hello, write by StringWriter, StringWriter inherit TextWriter");
TextReader StringReader = new StringReader (StringWriter. ToString ());
Console.WriteLine ("From {0}--{1}", StringReader. GetType (). Name, StringReader. ReadToEnd ());
Console.WriteLine ();

3.StreamWriter (reader) also inherits TextWriter (reader), which mainly handles data in the form of streams, such as memory streams, file streams, series port streams, and media video streams. Example:

using (TextWriter StreamWriter = new StreamWriter ("Mytext.txt"))
{
StreamWriter. Write ("Hello, write by StreamWriter, StreamWriter inherit TextWriter");
}
TextReader StreamReader = new StreamReader ("Mytext.txt");
Console.WriteLine ("From {0}--{1}", StreamReader. GetType (). Name, StreamReader. ReadToEnd ());
Console.ReadLine ();

4.BinaryWriter (reader) does not inherit TextWriter (reader), writes (reads) the primitive type in binary form, and supports a specific encoding, which has a broad form of support because it reads writes in binary mode. Example:

BinaryWriter (Reader)
Const string filename = "Test.txt";
if (file.exists (filename))
{
Console.WriteLine ("{0} file already exists! ", filename);
}
FileStream fs = new FileStream (filename, filemode.create);
BinaryWriter bw = new BinaryWriter (FS);
String str = environment.currentdirectory + @ "QQ. Exe.lnk ";
Bw. Write (str);
Bw. Close ();
Fs. Close ();
Start reading now
FS = new FileStream (filename, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader (FS);
Long size = br. Basestream.length;
byte[] ByteSize = new Byte[size];
ByteSize = Br. Readbytes ((int) size);
Console.WriteLine (ByteSize. ToString ());
Br. Close ();
Fs. Close ();
Console.ReadLine ();

Here is another example of BinaryWriter writing XML information:

Const string filename = "Xmlwriter1.xml";
FileStream fs = new FileStream (filename, filemode.create);
BinaryWriter bw = new BinaryWriter (FS);
XmlElement ee = xdoc. DocumentElement;
if (ee! = null)
{
Bw. Write (EE. OuterXml);
}
Else
{
Console.WriteLine ("Failed to get Object! ");
}
Bw. Close ();
Fs. Close ();

C # Learning Notes---Differentiate between stringwriter (reader) and StreamWriter (reader), TextWriter (reader), BinaryWriter (reader)

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.