C# tutorials > Input/Output (I/O) and Networking > .NET Streams and File I/O > Reading and writing text files (`StreamReader`, `StreamWriter`, `File.ReadAllText`, `File.WriteAllText`)
Reading and writing text files (`StreamReader`, `StreamWriter`, `File.ReadAllText`, `File.WriteAllText`)
This tutorial explores different methods in C# for reading and writing text files using the .NET framework. We will cover `StreamReader` and `StreamWriter` for handling streams of characters, and `File.ReadAllText` and `File.WriteAllText` for simpler, all-in-one operations. Understanding these techniques is fundamental for file manipulation in C# applications.
Writing to a Text File using `StreamWriter`
This snippet demonstrates writing to a text file named 'example.txt' using `StreamWriter`. The `using` statement ensures that the `StreamWriter` object is properly disposed of, even if an exception occurs, preventing resource leaks. `writer.WriteLine()` writes a line of text to the file, and a newline character is automatically appended. The `try-catch` block handles potential exceptions that might occur during file writing, such as the file not being found or insufficient permissions.
using System;
using System.IO;
public class StreamWriterExample
{
public static void Main(string[] args)
{
string filePath = "example.txt";
try
{
using (StreamWriter writer = new StreamWriter(filePath))
{
writer.WriteLine("This is the first line.");
writer.WriteLine("This is the second line.");
writer.WriteLine("Hello, World!");
}
Console.WriteLine("Successfully wrote to file: " + filePath);
}
catch (Exception ex)
{
Console.WriteLine("An error occurred: " + ex.Message);
}
}
}
Reading from a Text File using `StreamReader`
This snippet reads from the 'example.txt' file using `StreamReader`. The `using` statement ensures proper disposal of the `StreamReader`. `reader.ReadLine()` reads a single line of text from the file, returning `null` when the end of the file is reached. The `while` loop continues to read and print lines until the end of the file. Error handling is included using a `try-catch` block to manage potential exceptions during file reading.
using System;
using System.IO;
public class StreamReaderExample
{
public static void Main(string[] args)
{
string filePath = "example.txt";
try
{
using (StreamReader reader = new StreamReader(filePath))
{
string line;
while ((line = reader.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
Console.WriteLine("Successfully read from file: " + filePath);
}
catch (Exception ex)
{
Console.WriteLine("An error occurred: " + ex.Message);
}
}
}
Writing to a Text File using `File.WriteAllText`
`File.WriteAllText` provides a simple way to write an entire string to a file. It overwrites the file if it already exists. The content to be written, including newline characters (`\n`), is passed as a string. The `try-catch` block handles potential exceptions during the file writing process.
using System;
using System.IO;
public class FileWriteAllTextExample
{
public static void Main(string[] args)
{
string filePath = "example.txt";
string content = "This is the first line.\nThis is the second line.\nHello, World!";
try
{
File.WriteAllText(filePath, content);
Console.WriteLine("Successfully wrote to file: " + filePath);
}
catch (Exception ex)
{
Console.WriteLine("An error occurred: " + ex.Message);
}
}
}
Reading from a Text File using `File.ReadAllText`
`File.ReadAllText` provides a quick and easy way to read the entire content of a text file into a single string. The returned string includes all the text from the file, including newline characters. A `try-catch` block is essential for handling exceptions, such as the file not existing or access being denied.
using System;
using System.IO;
public class FileReadAllTextExample
{
public static void Main(string[] args)
{
string filePath = "example.txt";
try
{
string content = File.ReadAllText(filePath);
Console.WriteLine("File Content:\n" + content);
Console.WriteLine("Successfully read from file: " + filePath);
}
catch (Exception ex)
{
Console.WriteLine("An error occurred: " + ex.Message);
}
}
}
Concepts Behind the Snippets
Real-Life Use Case Section
Imagine you are building a log analysis tool. You could use `StreamReader` to incrementally read through a large log file, parsing each line and identifying specific events or errors. Alternatively, if you want to read a small configuration file, `File.ReadAllText` might be more convenient. For generating reports, you can use `StreamWriter` or `File.WriteAllText` to output the formatted data to a file.
Best Practices
Interview Tip
Be prepared to discuss the difference between `StreamReader`/`StreamWriter` and `File.ReadAllText`/`File.WriteAllText`. Understand when to use each approach based on file size and the need for incremental processing. Also, be ready to explain the importance of resource management and exception handling when working with file I/O.
When to Use Them
Memory Footprint
Alternatives
Pros of `StreamReader`/`StreamWriter`
Cons of `StreamReader`/`StreamWriter`
Pros of `File.ReadAllText`/`File.WriteAllText`
Cons of `File.ReadAllText`/`File.WriteAllText`
FAQ
-
What happens if the file specified in `StreamReader` or `StreamWriter` does not exist?
For `StreamReader`, if the file doesn't exist, it will throw a `FileNotFoundException` when you attempt to create the `StreamReader` object. For `StreamWriter`, if the file doesn't exist, it will create a new file at the specified path. If the file exists, it will be overwritten by default (unless you specify `true` for the `append` parameter in the constructor). -
How do I append to an existing file using `StreamWriter`?
To append to an existing file using `StreamWriter`, use the constructor that takes a `filePath` and a boolean `append` parameter. Set `append` to `true`. Example: `using (StreamWriter writer = new StreamWriter("example.txt", true))`. -
What encoding does `StreamReader` and `StreamWriter` use by default?
By default, `StreamReader` and `StreamWriter` use UTF-8 encoding. However, it's best practice to explicitly specify the encoding to avoid potential issues with different character sets. You can specify the encoding in the constructor: `new StreamWriter(filePath, false, Encoding.UTF8)` or `new StreamReader(filePath, Encoding.UTF8)`. -
How to handle large files efficiently with `StreamReader`?
With `StreamReader`, read the file line by line or in chunks. Avoid loading the entire file into memory at once. Use a loop to process each line or chunk as it's read. Also, ensure you dispose of the `StreamReader` object properly using a `using` statement.