Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Streams and Byte Streams in C#
A file is a collection of data stored in a disk with a specific name and a directory path. When a file is opened for reading or writing, it becomes a stream. Streams in C# provide a unified interface for reading and writing data, whether it's from files, memory, or network connections.
The type of streams includes −
-
Byte Streams − They handle raw binary data and include Stream, FileStream, MemoryStream, and BufferedStream.
-
Character Streams − They handle text data and include TextReader, TextWriter, StreamReader, StreamWriter, and other text-based streams.
Byte streams treat data as sequences of bytes, making them suitable for handling binary files like images, executables, or any non-text data. The System.IO namespace provides all the necessary classes for file and stream operations.
Stream Base Class Properties
The Stream class is the base class for all byte stream classes. The following are its key properties −
-
CanRead − Whether the stream supports reading operations
-
CanWrite − Whether the stream supports writing operations
-
CanSeek − Whether the stream supports seeking to different positions
-
Length − Gets the length of the stream in bytes
-
Position − Gets or sets the current position within the stream
Using FileStream for Binary Data
Example
using System;
using System.IO;
using System.Text;
class Program {
public static void Main() {
string filePath = "example.dat";
// Write binary data to file
using (FileStream fs = new FileStream(filePath, FileMode.Create)) {
byte[] data = Encoding.UTF8.GetBytes("Hello, World!");
fs.Write(data, 0, data.Length);
Console.WriteLine("Data written to file");
}
// Read binary data from file
using (FileStream fs = new FileStream(filePath, FileMode.Open)) {
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
string result = Encoding.UTF8.GetString(buffer);
Console.WriteLine("Data read from file: " + result);
}
// Clean up
File.Delete(filePath);
}
}
The output of the above code is −
Data written to file Data read from file: Hello, World!
Using MemoryStream for In-Memory Operations
Example
using System;
using System.IO;
using System.Text;
class Program {
public static void Main() {
// Create a MemoryStream
using (MemoryStream ms = new MemoryStream()) {
byte[] data = Encoding.UTF8.GetBytes("Memory Stream Example");
// Write to memory stream
ms.Write(data, 0, data.Length);
Console.WriteLine("Length: " + ms.Length);
Console.WriteLine("Position: " + ms.Position);
// Reset position to beginning
ms.Position = 0;
// Read from memory stream
byte[] buffer = new byte[ms.Length];
ms.Read(buffer, 0, buffer.Length);
string result = Encoding.UTF8.GetString(buffer);
Console.WriteLine("Content: " + result);
}
}
}
The output of the above code is −
Length: 21 Position: 21 Content: Memory Stream Example
System.IO Classes Overview
The System.IO namespace provides various classes for file and stream operations −
| Class | Description | Primary Use |
|---|---|---|
| BinaryReader | Reads primitive data from a binary stream | Reading binary files |
| BinaryWriter | Writes primitive data in binary format | Writing binary files |
| BufferedStream | Provides buffering for read and write operations | Performance optimization |
| FileStream | Provides access to files with read/write/seek capabilities | File I/O operations |
| MemoryStream | Provides access to streamed data stored in memory | In-memory data processing |
| StreamReader | Reads characters from a byte stream using encoding | Reading text files |
| StreamWriter | Writes characters to a stream using encoding | Writing text files |
Conclusion
Streams in C# provide a unified approach to handle data flow operations. Byte streams like FileStream and MemoryStream are essential for binary data manipulation, while the Stream base class provides common functionality and properties. Understanding these concepts is crucial for effective file I/O and data processing in C# applications.
