Welcome to another lesson, where we'll explore a fundamental technique in text data manipulation: reading files line-by-line using C#. In many real-world applications, processing data one line at a time is crucial for effective data management, particularly when dealing with large files like logs or data streams. By the end of this lesson, you'll understand how to efficiently read and process file data line-by-line using C#.
File handling can be done using the System.IO.File
class and specifically with File.ReadLines
for reading files lazily line-by-line. This method reads lines from a text file lazily, returning an IEnumerable<string>
that you can iterate over. This method is ideal for processing each line of the file without loading the entire file into memory:
C#1foreach (var line in File.ReadLines(filePath)) 2{ 3 // Process each line 4}
filePath
: The path to the file from which to read lines.line
: Represents each line read from the file.
This method is perfect for handling large files line-by-line efficiently.
To read a file line-by-line in C#, use the File.ReadLines()
method and iterate over the results with a foreach
loop. Let's see this in action:
C#1using System; 2using System.IO; 3 4class Program 5{ 6 static void Main(string[] args) 7 { 8 string filePath = "data.txt"; 9 Console.WriteLine("Reading file line-by-line:"); 10 11 foreach (string line in File.ReadLines(filePath)) 12 { 13 Console.WriteLine(line); // Output each line to the console 14 } 15 } 16}
Here, File.ReadLines(filePath)
reads lines from data.txt
one-by-one. Each line is stored in the line
variable and then printed to the console.
Imagine needing to process a set of numbers stored in a file—reading each integer and calculating their sum. This is a practical task you can accomplish using ReadLines()
with ease.
Assume numbers.txt
contains:
Plain text110 220 330 440
The following code reads integers from this file and calculates their sum:
C#1using System; 2using System.IO; 3 4class Program 5{ 6 static void Main(string[] args) 7 { 8 string filePath = "numbers.txt"; 9 int totalSum = 0; 10 11 foreach (string line in File.ReadLines(filePath)) 12 { 13 if (int.TryParse(line, out int number)) 14 { 15 totalSum += number; // Add the integer to totalSum 16 } 17 } 18 19 Console.WriteLine("The sum of the numbers is: " + totalSum); 20 } 21}
- Reading Numbers: Each line is parsed to an integer using
int.TryParse()
, ensuring safe conversion. - Calculating Sum: The integers are added together in the
totalSum
variable.
After executing this code, the output displays the total sum of the numbers:
Plain text1The sum of the numbers is: 100
In this lesson, you gained the skills to read a text file line-by-line using C# — a fundamental technique for processing large datasets efficiently. You've learned to manage file I/O operations using the File.ReadLines
method and how to handle each line effectively.
Now, you're ready to dive into practice exercises where you can apply these concepts and strengthen your understanding. Continue to build on these skills as we explore further parsing techniques in future lessons. Keep up the great work!