Reading from a Text File in C#
Reading data from a text file in C# is straightforward, using the StreamReader
class. This class allows you to read the contents of a text file line-by-line or all at once. Let’s look at how to read data from a file effectively, and handle common issues like file existence and end-of-file detection.
Example Code
Add the following code to your project after the code for writing to the file:
Explanation
- Opening the Stream: The
StreamReader
object,sr
, is initialized with the name of the file,text.txt
. This file should exist in the current directory. - Reading Line-by-Line: The first line is read using
ReadLine()
and stored in thestrIn
variable. Thewhile
loop continues reading until the end of the file is reached (whenstrIn
becomesnull
). - Printing to Console: Each line read from the file is printed to the console using
Console.WriteLine(strIn);
.
Combining Operations
You can simplify the loop condition by combining the read operation directly in the while
condition:
This version is more concise and is a common pattern for reading lines from a file until the end is reached.
Checking for End of File with Peek()
An alternative to using ReadLine()
and checking for null
is to use the Peek()
method:
- How
Peek()
Works:Peek()
returns the next character to be read without removing it from the stream. If the next character is-1
, it means the end of the file has been reached.
Handling Exceptions
When working with files, it’s essential to handle exceptions that may occur if the file is missing or inaccessible. The following example demonstrates how to use a try...catch
block to manage these scenarios gracefully:
Exception Types to Handle
FileNotFoundException
: This exception is thrown if the file specified cannot be found.DirectoryNotFoundException
: This exception is thrown if the directory specified in the file path does not exist.IOException
: A general exception for I/O errors, such as file access being denied or the file being used by another process.
Best Practices
- Wrap File Operations in
try...catch
: Always wrap your file operations in atry...catch
block to handle exceptions gracefully and prevent your program from crashing unexpectedly. - Use
using
Statements: Ensure that streams are properly closed by using theusing
statement, which automatically disposes of the stream when the block is exited. - Check File Existence: Before attempting to read from a file, consider checking if the file exists using
File.Exists(filename)
.
Exercise
- Modify the Program: Change the file name in the example code to a file that does not exist on your system. Run the program and observe how the exceptions are handled.
- Experiment with
Peek()
: Replace thewhile
loop condition with thePeek()
method and observe the differences in behavior. - Read Entire File: Modify the code to read the entire contents of the file into a single string using
ReadToEnd()
and then print it out.