1. IO Streams
-
Text Stream: Used to read and write character data (text) from sources like files, memory, and network using encoding-aware classes such as StreamReader and StreamWriter.
-
Binary Stream: Used for reading and writing raw binary data (bytes) using classes like BinaryReader and BinaryWriter.
Features
-
Both text and binary streams inherit from the abstract base class System.IO.Stream.
-
Provide sequential data access (read/write)
-
Support synchronous and asynchronous operations
-
Text streams handle encoding automatically, binary streams deal with raw bytes.
2. System.IO and Base Classes of Stream
System.IO Namespace
-
Contains types to perform input/output operations on streams and files.
-
Includes classes like Stream, File, FileInfo, Directory, DirectoryInfo, Path, FileStream, StreamReader, StreamWriter, BinaryReader, and BinaryWriter.
Stream Class (Base Class)
Definition
-
An abstract class that provides a generic view of a sequence of bytes.
Key Features
-
Supports reading, writing, and seeking operations.
-
Has properties CanRead, CanWrite, CanSeek, Position, Length.
-
Implements IDisposable for resource management.
-
Supports synchronous and asynchronous methods (Read, ReadAsync, Write, WriteAsync).
-
Derived classes must implement Read and Write methods.
3. Console I/O Streams
Console Class (System Namespace)
-
Provides standard input, output, and error streams for console applications.
-
Key properties: Console.In (TextReader), Console.Out (TextWriter), Console.Error (TextWriter).
-
Supports reading from keyboard and writing to screen.
-
Allows redirection of input/output to other streams.
-
Supports methods like ReadLine(), WriteLine(), ReadKey() for interaction.
4. Working with File System
4.1 File Class
-
Provides static methods for creating, copying, deleting, moving, and opening files.
-
Works with file paths as strings.
-
Simple and easy-to-use for common file operations.
4.2 FileInfo Class
-
Represents a file in the file system with properties and instance methods.
-
Allows detailed information (size, attributes, dates) retrieval and instance operations.
-
Better suited for repeated operations on the same file as it holds state.
4.3 Directory Class
-
Provides static methods for creating, moving, and enumerating directories and subdirectories.
-
Operates with directory paths as strings.
-
Includes methods to check existence and get directory contents.
4.4 DirectoryInfo Class
-
Represents a directory and exposes instance methods and properties for detailed operations.
-
Supports enumeration of files and subdirectories with rich metadata.
-
Optimized for repeated access to a specific directory.
Code Examples
# Install .NET CLI tool for file operations (example)
dotnet add package System.IO
using System;
using System.IO;
class Program
{
static void Main()
{
// Open the source file for reading
using (StreamReader reader = new StreamReader("source.txt"))
{
// Open the destination file for writing (will overwrite if exists)
using (StreamWriter writer = new StreamWriter("dest.txt"))
{
string line;
// Read each line from the source until end of file
while ((line = reader.ReadLine()) != null)
{
// Write the line into the destination file
writer.WriteLine(line);
}
}
}
Console.WriteLine("File copied successfully!");
}
}
using System;
using System.IO;
class StreamExamples
{
private static string filePath = "sample.txt";
public static void TextStreamExample()
{
// Writing text to a file using StreamWriter (text stream)
using (StreamWriter writer = new StreamWriter(filePath))
{
writer.WriteLine("Hello, world!");
writer.WriteLine("This is a text stream example.");
}
// Reading text from a file using StreamReader (text stream)
using (StreamReader reader = new StreamReader(filePath))
{
string content = reader.ReadToEnd();
Console.WriteLine("File Contents:");
Console.WriteLine(content);
}
}
public static void ConsoleIOExample()
{
Console.WriteLine("Enter your name:");
string name = Console.ReadLine();
Console.WriteLine($"Hello, {name}!");
}
public static void FileAndDirectoryExample()
{
// Using File class to create a file
string path = "testfile.txt";
File.WriteAllText(path, "Sample text");
// Using FileInfo class
FileInfo fileInfo = new FileInfo(path);
Console.WriteLine($"File size: {fileInfo.Length} bytes");
// Using Directory class
string dir = "TestDir";
Directory.CreateDirectory(dir);
// Using DirectoryInfo class
DirectoryInfo dirInfo = new DirectoryInfo(dir);
Console.WriteLine($"Directory exists: {dirInfo.Exists}");
}
static void Main()
{
TextStreamExample();
BinaryStreamExample();
ConsoleIOExample();
FileAndDirectoryExample();
}
}
public static void BinaryStreamExample()
{
byte[] data = new byte[] { 0, 1, 2, 3, 4, 5 };
// Write binary data to a file
using (FileStream fs = new FileStream("binarydata.bin", FileMode.Create))
{
fs.Write(data, 0, data.Length);
}
// Read binary data from a file
using (FileStream fs = new FileStream("binarydata.bin", FileMode.Open))
{
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
Console.WriteLine("Binary Data:");
foreach (var b in buffer)
{
Console.Write(b + " ");
}
Console.WriteLine();
}
}