C# > Data Access > File I/O > StreamReader and StreamWriter
Asynchronous File Reading and Writing with StreamReader and StreamWriter
This snippet demonstrates how to use asynchronous operations with `StreamReader` and `StreamWriter` in C# to prevent blocking the main thread, especially when working with large files. Asynchronous file I/O improves application responsiveness and user experience.
Core Concepts
Asynchronous operations allow your application to continue executing other tasks while waiting for a long-running operation (like file I/O) to complete. This prevents the application from becoming unresponsive. `StreamReader` and `StreamWriter` provide asynchronous methods like `ReadLineAsync` and `WriteAsync` that can be used with the `async` and `await` keywords to perform non-blocking file operations.
Asynchronous File Writing
This code shows how to write to a file asynchronously using `StreamWriter` and the `WriteLineAsync` method. The `async` keyword is used in the `Main` method signature, and the `await` keyword is used before each call to `WriteLineAsync`. This allows the application to continue executing other code while the file write operation is in progress. The `using` statement still ensures that the `StreamWriter` is properly disposed of.
using System;
using System.IO;
using System.Threading.Tasks;
public class AsyncFileWriterExample
{
public static async Task Main(string[] args)
{
string filePath = "async_example.txt";
try
{
// Create a StreamWriter to write to the file asynchronously
using (StreamWriter writer = new StreamWriter(filePath))
{
await writer.WriteLineAsync("This is the first line (async).");
await writer.WriteLineAsync("This is the second line (async).");
await writer.WriteLineAsync("Hello, World! (async).");
}
Console.WriteLine("Data written to file successfully (async).");
}
catch (Exception ex)
{
Console.WriteLine("An error occurred: " + ex.Message);
}
}
}
Asynchronous File Reading
This code demonstrates how to read from a file asynchronously using `StreamReader` and the `ReadLineAsync` method. Similar to the writing example, the `async` and `await` keywords are used to perform the read operation without blocking the main thread. The `ReadLineAsync` method returns a `Task
using System;
using System.IO;
using System.Threading.Tasks;
public class AsyncFileReaderExample
{
public static async Task Main(string[] args)
{
string filePath = "async_example.txt";
try
{
// Create a StreamReader to read from the file asynchronously
using (StreamReader reader = new StreamReader(filePath))
{
string line;
// Read the file line by line asynchronously until the end
while ((line = await reader.ReadLineAsync()) != null)
{
Console.WriteLine(line);
}
}
Console.WriteLine("Data read from file successfully (async).");
}
catch (Exception ex)
{
Console.WriteLine("An error occurred: " + ex.Message);
}
}
}
Real-Life Use Case
Asynchronous file I/O is particularly useful in GUI applications or web servers where responsiveness is critical. For example, a desktop application that processes large log files can use asynchronous file reading to avoid freezing the UI while the file is being processed. A web server can use asynchronous file writing to log user activity without slowing down the response to client requests.
Best Practices
Interview Tip
Be prepared to explain the benefits of asynchronous programming, especially in the context of file I/O. Discuss how asynchronous operations can improve application responsiveness and prevent UI freezes. Also, be ready to explain the roles of the `async` and `await` keywords.
When to Use Them
Use asynchronous `StreamReader` and `StreamWriter` when you need to perform file operations without blocking the main thread. This is especially important in UI applications, web servers, and any other application where responsiveness is critical.
Memory Footprint
The memory footprint is similar to synchronous `StreamReader` and `StreamWriter`, depending on the buffering. Asynchronous operations don't directly affect memory usage but prevent blocking the calling thread, enabling more efficient use of overall system resources.
Alternatives
Pros
Cons
FAQ
-
What happens if an exception occurs during an asynchronous file operation?
If an exception occurs during an asynchronous file operation, it will be caught by the `try-catch` block, just like in synchronous operations. The exception will be wrapped in a `Task` object. -
How do I cancel an asynchronous file operation?
You can use a `CancellationToken` to cancel an asynchronous file operation. Pass the `CancellationToken` to the asynchronous method and check its `IsCancellationRequested` property periodically. If cancellation is requested, throw a `TaskCanceledException`.