Dear Friends following article will help you to under to understand the stream class in .NET
Streams are another very common type because they are the means for reading from and writing to the disk and communicating across the network. The System.IO.Stream type is the base type for all task-specific stream types. Table 1-5 shows some of the most commonly used stream types. In addition, network streams are found in the System.Network.Sockets namespace, and encrypted streams are found in the System.Security.Cryptography namespace.
Table 1-5: Common Stream Types System.IO Type
Use to
FileStream
Create a base stream used to write to or read from a file
MemoryStream
Create a base stream used to write to or read from memory
StreamReader
Read data from the stream
StreamWriter
Write data to the stream
The simplest stream classes are StreamReader and StreamWriter, which enable you to read and write text files. You can pass a filename as part of the constructor, enabling you to open a file with a single line of code. After you have processed a file, call the Close method so that the file does not remain locked. The following code, which requires the System.IO namespace, demonstrates how to write to and read from a text file:
' VB
' Create and write to a text file
Dim sw As StreamWriter = New StreamWriter("text.txt")
sw.WriteLine("Hello, World!")
sw.Close
' Read and display a text file
Dim sr As StreamReader = New StreamReader("text.txt")
Console.WriteLine(sr.ReadToEnd)
sr.Close
// C#
// Create and write to a text file
StreamWriter sw = new StreamWriter("text.txt");
sw.WriteLine("Hello, World!");
sw.Close();
// Read and display a text file
StreamReader sr = new StreamReader("text.txt");
Console.WriteLine(sr.ReadToEnd());
sr.Close();
Wednesday, May 27, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment