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)