Stream Classes
System.IO namespace
Reading:
The transfer of data from a stream to a data structure, such as an array of bytes.
Writing:
The transfer of data from a data structure to a stream.
Seeking:
The querying and modifying the current position within a stream.
File Stream
Stream use the concept of an internal system pointer. When you open a stream, the stream pointer is normaly positioned at the first byte of the stream.
Visualize a stream as a bit array that may be attached to some memory buffer or to some disk files or devices.
Start point:
SeekOrigin.Begin SeekOrigin.Current SeekOrigin.Ends.Seek(13, SeekOrigin.Begin)
If you want to seek from SeekOrigin.End, you should supply a negative value.
From SeekOrigin.Current you can supply positive and negative value depending on which direction you want to go.
s.SetLength();
arbitrarily set the length
A stream must support both writing and seeking for SetLength to work.
FileMode.Append
can be only used in conjunction with FileAccess.Write.
StreamReader and StreamWriter
FileStream s = new FileStream("Bar.txt");
StreamWriter w = new StreamWriter(s);
w.Write("Hello World");
w.Close();
StreamReader s = new StreamReader(s):Closing the StreamWriter/StreamReader also closes the underlying FileStream.
Encoding
StreamWriter w = new StreamWriter(s, System.Text.Encoding.XXXX);
Memory and Buffered Stream
Both can be associated with a stream of other kind -- such as a file -- if required, and thus both can be used to act as buffer between memory and persisitent storage.
MemoryStream class offers method to write to other stream;
Buffered stream class is associated with other stream upon constructor and when you close the BufferedStream, its content will flushed to the assocated stream.If write more data to a stream is already up to its capacity, it will dynamically resize the buffer.
String Readers and Writers
Using string writer to build mutable string in memory and extract from it either a String or a StringBuilder class.
Flush method in StringWriter class, flushing the stream, won't flush its underlying encoder unless you explicitly call Close. Close the stream will automatically flush it and will ready it for destruction by calling Dispose.
Binary Reader and Writer
Binary Writers allow to write data to a stream without text interpolation, and the data is written in Binary Form.
File System Cloasses
Directory and DirectoryInfo
DirectInfo dir = new DirectoryInfo(Directory.GetCurrentDirectory);
Directory class offers more of less method parallels the instance methods in the DirectoryInfo class.
File and FileInfo
File with only static method and FileInfo with only instance method.
We can use method of FileInfo in place of the overloaded FileStream constructor. We can reuse the same FileInfo object and simply regenerate a FileStream object.
OpenText, CreateText, AppendText method will return StreamReader or StreamWriter objects.
Parsing Paths
Pass class is designed to enable you to easily perform comon operations such as determine whether a filename extension is part of a path and combing two strings into one pathname.
Provide the location of a file or directory, it doesn't necessarily point to a location on disk.
All members of the Path class are static.
Can contain absolute and relative loaction information.
Nonconsole Use of Streams
Windows OpenFile Dialog
It's entirly possible to use the class in a console application.
Reading Web Pages
Uri uri = new Uri("http://.......");
WebRequest req = WebRequest.Create(Uri);
WebResponse resp = req.GetReponse();
Stream str = resp.GetResponseStream();
Serialization
To supoort user defined types.
Use attribute to mark elements of your class Serializable or NonSerialized.
Serialize, Deserialize ===> IFormatter interface
BinaryFormatter: a simple binary data string with some additional type information.
SoapFormatter: format data stream as XML.Serialize: both Field member value and type information
Serialization doesn't just store data, it stores enough additional type info to reconstruct the correct object dynamially from the seialized stream, has no problem in reading and writing private fields in a class.
Reversion of serialization process not only applies to a single serialized class object, it also applies to an entire graph of connected object.
Serializing with XMLSerializer
The Serializable and NonSerialized attribute will be ignored.
XMLIgnore attribute behavior like NonSerialized.
has no unsafe access to private members.
require class have a default constructor.
XMLSerializer object is tied to one class.
eliminates all the Soap-specific extras.
Implementing ISerializable
ISerializable interface offers one method: GetObjectData
public virtual void GetObjectData(SerializtionInfo s, StreamContext c)
a constructor has the same parameters list as GetObjectData, should be declared private or protected
the constructor will be used as part of deserialization.