Using the Stream class to manage the byte stream
Using the FileStream class to manage file data
Using the MemoryStream class to manage memory data
Using the Bufferedsream class to improve flow performance
3.1FileStream
MemoryStream
Seek positioning, addressing
BufferedStream
3.2 Managing Application Data
Text, Stream, string, and binary data
Managing text data and strings
(1) TextReader Class (abstract class)
(2) TextWriter Class (abstract class)
Managing strings
Managing binary data with BinaryReader and Binarywrter
3.2.1.file Operations
Really associated to a file //streamreader to a text file: Read <--parent class: Stream //streamwriter to text file: Write <--parent class: Stream string file = @ "F:\text.txt"; FileStream fs = File.Open (File, filemode.openorcreate); StreamWriter SW = new StreamWriter (fs); Sw. WriteLine ("Now: {0},", DateTime.Now); Sw. Flush (); Sw. Close (); StreamReader sr = new StreamReader (File.Open (File, FileMode.Open)); Console.WriteLine (Sr. ReadToEnd ());
3.2.2.string Manipulation
In memory, string is manipulated as a file to //stringreader to string: Read <--textreader //stringwriter to string: Write <--textwiter StringBuilder sb = new StringBuilder (); StringWriter SWS = new StringWriter (SB); SwS. WriteLine (123 + 321); SwS. WriteLine (true); StringReader SRR = new StringReader (sb.) ToString ()); Console.WriteLine (SRR. ReadToEnd ());
3.2.3.Binary Operations
Binary BinaryWriter bw = new BinaryWriter (File.Open (@ "F:\a.dat", FileMode.OpenOrCreate)); Bw. Write (true); Bw. Write (' A '); Bw. Write (123); Bw. Flush (); Bw. Close (); Read in order binaryreader br = new BinaryReader (File.Open (@ "F:\a.dat", FileMode.Open)); Console.WriteLine (Br. Readboolean ()); Corresponds to True Console.WriteLine (Br. ReadChar ()); Corresponds to ' A ' Console.WriteLine (Br. ReadInt32 ()); Corresponds to 123
(GO). NET IO: Byte stream