In C# programming language, streams play a vital role in handling data input and output operations. A stream is an abstract representation of a sequence of data, which can be read from or written to. It provides a convenient way to handle various types of data, such as files, network connections, and memory buffers.
In C#, there are two main types of streams: input streams and output streams. Input streams allow you to read data from a source, while output streams enable you to write data to a destination.
Input streams provide a way to read data from a source. They inherit from the abstract base class System.IO.Stream
, which contains the common methods and properties for reading from a stream. Some commonly used input stream classes in C# include:
FileStream
: This class enables you to read data from a file on disk.NetworkStream
: It allows you to read data from and write data to a network socket.MemoryStream
: This class offers a way to read data from and write data to a managed memory buffer.Output streams, on the other hand, provide a means to write data to a destination. Like input streams, they also inherit from the base class System.IO.Stream
. Some commonly used output stream classes in C# are:
FileStream
: This class enables you to write data to a file on disk.NetworkStream
: It allows you to read data from and write data to a network socket.MemoryStream
: This class offers a way to read data from and write data to a managed memory buffer.While streams provide the basic functionality to read from and write to data sources, stream readers and writers offer a higher-level abstraction that simplifies the process. They provide additional methods specifically designed for reading and writing textual information.
StreamReader
is a class that extends the functionality of input streams by providing methods to read text from a stream. It automatically handles the conversion of bytes to characters and provides methods such as Read
, ReadLine
, and ReadToEnd
to facilitate reading of text data.
To create a StreamReader
object, you need to pass an input stream as a parameter. Here's an example of creating a StreamReader
to read from a file:
using System;
using System.IO;
class Program
{
static void Main()
{
using (FileStream fileStream = new FileStream("data.txt", FileMode.Open))
using (StreamReader reader = new StreamReader(fileStream))
{
string line;
while ((line = reader.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
}
In the above example, the using
statement ensures that the StreamReader
and FileStream
objects are properly disposed of after use.
Similarly to StreamReader
, StreamWriter
is a class that extends the functionality of output streams by providing methods to write text to a stream. It offers methods such as Write
, WriteLine
, and Flush
for writing textual data.
To create a StreamWriter
object, you need to pass an output stream as a parameter. Here's an example of creating a StreamWriter
to write to a file:
using System;
using System.IO;
class Program
{
static void Main()
{
using (FileStream fileStream = new FileStream("output.txt", FileMode.Create))
using (StreamWriter writer = new StreamWriter(fileStream))
{
writer.WriteLine("Hello, world!");
writer.WriteLine("This is a sample text file.");
}
}
}
Again, the using
statement ensures that the StreamWriter
and FileStream
objects are properly disposed of after use.
Streams and their corresponding stream readers and writers are essential components of the C# programming language when it comes to handling input and output operations. They provide convenient and efficient ways to read and write data from various sources. Whether you need to read from a file or write to a network socket, streams and stream readers/writers are your go-to tools for data manipulation in C#.
noob to master © copyleft