Even when I already knew how to pedal, I could not remember the inheritance hierarchy and how to use classes like Stream, StreamWriter, HttpWriter in .Net. I decided this in a simple way - I wrote them all out (basic), wrote a few words about each.
For those who are familiar with the topic, absolutely nothing new will be here.
Here we have the main classes:
Stream
- abstract
Provides a generic representation of a sequence of bytes.
')
Memory stream
Creates a stream whose backup storage is memory.
And reads and writes.
Such a means to handle byte [].
A piece of code:
byte [] buffer = new byte [] {11,12,13,14,15,16,17,18,19,20};
byte [] buffer2 = new byte [] {21,22,23};
MemoryStream memStream = new MemoryStream (buffer);
memStream.WriteByte (66);
memStream.WriteByte (77);
int adsfs = memStream.ReadByte ();
int adsfs2 = memStream.ReadByte ();
memStream.Position = 5;
memStream.Write (buffer2, 0, 2);
memStream.Seek (2, SeekOrigin.Current);
Interestingly, both read and write equally move the position in the stream to the number of written / read bytes.
File stream
All the same, just write / read from file
A piece of code:
FileStream fileStream = new FileStream ("file33.txt", FileMode.Open);
byte [] buffer2 = new byte [] {71, 72, 73};
var b1 = fileStream.ReadByte ();
var b2 = fileStream.ReadByte ();
var b3 = fileStream.ReadByte ();
fileStream.WriteByte ((byte) b3);
fileStream.WriteByte ((byte) b3);
// and all the properties
var b = fileStream.CanRead;
b = fileStream.CanWrite;
b = fileStream.CanSeek;
fileStream.Position = 106;
fileStream.Write (buffer2, 0, 3);
fileStream.Seek (1, SeekOrigin.End);
fileStream.WriteByte (76);
fileStream.WriteByte (76);
fileStream.WriteByte (76);
fileStream.WriteByte (76);
ReadByte () returns an int to understand when the read was unsuccessful (-1).
TextReader
- abstract
Stream add-in that allows you to read (read only) characters, not bytes.
Represents a reader that allows you to read consecutive character sets.
Streamream reader
A piece of code:
byte [] buffer = new byte [] {71,72,73,74,75,76,77,78,79,80, 80,80,80};
byte [] buffer2 = new byte [] {81,82,83};
MemoryStream memStream = new MemoryStream (buffer);
using (StreamReader strReader = new StreamReader (memStream))
{
var sd = strReader.Peek ();
var sdd = strReader.Peek ();
var sded = strReader.Peek ();
var dasd = strReader.Read ();
string s = ((char) dasd) .ToString ();
var das2d = strReader.Read ();
var das3d = strReader.Read ();
strReader.BaseStream.Position = 8;
var vsd = strReader.ReadToEnd ();
}
Everything is now simple, except that the Position jumps - because the data is being buffered.
StringReader
A piece of code:
string s = "SDAFASDF AF dfsdfsd fas";
StringReader stringReader = new StringReader (s);
var sdf = stringReader.Read ();
var sddf = stringReader.Read ();
var sddasdf = stringReader.Read ();
var sdsssf = stringReader.Read ();
var sdfaa = stringReader.Read ();
var sdfg = stringReader.ReadLine ();
Textwriter
- abstract
Represents a writer that allows you to write consecutive character sets.
Streamwriter
Add-on Stream to write to the stream. Derived from the abstract TextWriter
A piece of code:
FileStream fileStream = new FileStream ("file33.txt", FileMode.Open);
fileStream.Seek (1, SeekOrigin.End);
StreamWriter strWriter = new StreamWriter (fileStream);
strWriter.Write ("sad");
strWriter.WriteLine ("sad");
strWriter.Write ("sad");
strWriter.Close ();
The position of the Stream does not change when recording.
A piece of code:
byte [] buffer = new byte [] {71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 80, 80, 80};
MemoryStream memStream = new MemoryStream (buffer);
StreamWriter strWriter = new StreamWriter (memStream);
strWriter.Write ("sad");
// strWriter.Write ("sasfagfsdghs h dfhdh d hd ddf d");
strWriter.Close ();
Xmlreader
Another add-on. Now over TextReader. It can be initialized as a string, as a stream, there and a textstream.
A piece of code:
FileStream fileStream = new FileStream ("file1.txt", FileMode.Open);
XmlReader reader = XmlReader.Create (fileStream);
StringBuilder output = new StringBuilder ();
XmlWriter writer = XmlWriter.Create (output);
// Parse the file and display each of the nodes.
while (reader.Read ())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
writer.WriteStartElement (reader.Name);
break;
case XmlNodeType.Text:
writer.WriteString (reader.Value);
break;
case XmlNodeType.XmlDeclaration:
case XmlNodeType.ProcessingInstruction:
writer.WriteProcessingInstruction (reader.Name, reader.Value);
break;
case XmlNodeType.Comment:
writer.WriteComment (reader.Value);
break;
case XmlNodeType.EndElement:
writer.WriteFullEndElement ();
break;
}
}
writer.Close ();
Quite strangely (but it seems like the fastest of all) reads xml. I mean, Xdocument or XMLDocument is much more convenient, but these classes, in fact, are further add-ins to this construct.
Xmlwriter
Represents a reader that provides fast, non-cached, data access.
Add-on TextWriter (StreamWriter) with methods of this type:
A piece of code:
using (XmlWriter writer = XmlWriter.Create (xmlFile, settings))
{
writer.WriteStartDocument (false);
writer.WriteComment ("This is a comment.");
writer.WriteStartElement ("books");
writer.WriteStartElement ("book");
writer.WriteElementString ("id", "1");
writer.WriteElementString ("name", "Apple Training Series: GarageBand 09");
writer.WriteElementString ("author", "Mary Plummer");
writer.WriteElementString ("price", "$ 39.99");
writer.WriteEndElement ();
writer.WriteEndElement ();
writer.Flush ();
}
}