This post will discuss how to read the contents of a file line by line in C#.
There are several ways to read the contents of a file line by line in C#. These are discussed below in detail:
1. Using File.ReadLines() method
The recommended solution to read a file line by line is to use the File.ReadLines() method, which optionally takes a specific character encoding. The following code example demonstrates its usage to read the lines of a file.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; using System.Collections.Generic; using System.IO; public class Example { public static void Main() { string fileName = @"C:\some\path\file.txt"; IEnumerable<string> lines = File.ReadLines(fileName); Console.WriteLine(String.Join(Environment.NewLine, lines)); } } |
The ReadLines throws an IOException if an I/O error occurs while opening the file and FileNotFoundException if the file is not found.
When working with huge files, ReadLines is very efficient as it returns an Enumerable, and we can start enumerating before the whole collection is returned.
2. Using File.ReadAllLines() method
We can also use the ReadAllLines() method to read a file line by line.
Unlike ReadLines, which returns an Enumerable, ReadAllLines returns a string array containing all lines of the file, and we must wait for the whole array of strings to be returned before accessing the array. Therefore, we should not use it with large files.
Like ReadLines, ReadAllLines optionally takes a specific character encoding and throws an IOException if an I/O error occurs while opening the specified file and FileNotFoundException if the source file is not found.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System; using System.IO; public class Example { public static void Main() { string fileName = @"C:\some\path\file.txt"; string[] lines = File.ReadAllLines(fileName); Console.WriteLine(String.Join(Environment.NewLine, lines)); } } |
The File class also offers the ReadAllText() method, which reads all the text in the file in one go. The following code example uses the ReadAllText() method to read all the text in the file in one operation and returns a string. To read the file line by line, split the string using String.Split() method with the newline as a delimiter.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System; using System.IO; public class Example { public static void Main() { string fileName = @"C:\some\path\file.txt"; string text = File.ReadAllText(fileName); string[] lines = text.Split(Environment.NewLine); foreach (string line in lines) { Console.WriteLine(line); } } } |
3. Using StreamReader.ReadLine() method
Another solution is to use the StreamReader.ReadLine() method. We can use it to read lines from a file until the end of the file is reached.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; using System.IO; public class Example { public static void Main() { string fileName = @"C:\some\path\file.txt"; using (StreamReader reader = new StreamReader(fileName)) { string line; while ((line = reader.ReadLine()) != null) { Console.WriteLine(line); } } } } |
We can also use the StreamReader.ReadToEnd() method, but it reads the whole file in a single operation.
The following code opens the text file using a stream reader, copies the contents to a string, then splits the string with newline as separator using the String.Split() method. Since the stream reader is declared and instantiated in a using statement, the Dispose() method is automatically called to flush and closes the stream.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
using System; using System.IO; public class Example { public static void Main() { string fileName = @"C:\some\path\file.txt"; using (StreamReader streamReader = File.OpenText(fileName)) { string text = streamReader.ReadToEnd(); string[] lines = text.Split(Environment.NewLine); foreach (string line in lines) { Console.WriteLine(line); } } } } |
That’s all about reading a file line-by-line with C#.