C# Program to Read the Content of a File Line by Line


Introduction

Here, we’ll be learning to write a C# program to Read the content of a file content line by line. There are various methods to do so. We will be discussing them one by one.

File handling is done in C#. In most cases, the file is used to store data. File handling or file management in layman’s terms the various processes such as making the file, reading from it, writing to it, appending it, and so on. The reading and writing of files are the two most common operations in file handling.

1. Using File.ReadLines() method

The first way to read the content of a file line by line is by using File.ReadLines() method. This method accepts a character encoding as an optional parameter.

Well, this process may throw some exceptions as IO exceptions. This is thrown if an I/O error occurs and another error FileNotFoundException is thrown when the file you try to open is not found.

There is also a con to this process as when you are working with big files, this technique is quite inefficient. This returns Enumerable by which we can begin enumerating before the entire collection is returned.

Algorithm

The algorithm below will give a step-by-step procedure to read the content of the file line by line using File.ReadLines() method.

The algorithm discussed below will lead us to create the program.

Step 1  Create a string that will store the name of the file path, remember this is an absolute path.

Step 2  Use IEumerable for getting the final results as the file is read line by line.

Step 3  By using Environment.Newline gets the newline string defined for the environment. And then the program ends.

Example

Following is the code for this process −

using System;
using System.Collections.Generic;
using System.IO;

public class Example {
   public static void Main() {
      string fileloc = @"D:\ttpt\loc
ew.txt"; IEnumerable<string> lines = File.ReadLines(fileloc); Console.WriteLine(String.Join(Environment.NewLine, lines)); } }

Output

This is a file.
Hello from tutorials point.

Firstly a variable of string type needs to be created which contains the address of the file location. After that the file is read line by line in the program.

2. Using File.ReadAllLines() Method

Another way to read the content of a file line by line is by using File.ReadAllLines() method. We should not be using ReadAllLines for big data files as ReadAllLines, unlike ReadLines which returns Enumerable, ReadAllLines gives a string array comprising all lines of the file and we must wait for the entire array of strings to be returned before accessing the array.

This method accepts a character encoding as an optional parameter similar to what ReadLines do. Well, this process may throw some exceptions as IO Exception. This is thrown if an I/O error occurs and another error FileNotFoundException is thrown when the file you try to open is not found.

Algorithm

The algorithm below will give a step-by-step procedure to read the content of the file line by line using File.ReadAllLines() method.

The algorithm discussed below lead us to create the program.

Step 1  Create a string that will store the name of the file path, remember this is an absolute path.

Step 2  Next by using ReadAllLines the program opens a text file, reads all of its lines, and then ends it.

Step 3 − By using Environment.Newline gets the newline string defined for the environment. And then the program ends.

Example

Following is the code for this process −

using System;
using System.IO;

public class Example {
   public static void Main() {
      string fileloc = @"D:\ttpt\loc
ew.txt"; string[] lines = File.ReadAllLines(fileloc); Console.WriteLine(String.Join(Environment.NewLine, lines)); } }

Output

This is a file.
Hello from tutorials point.

Firstly a variable of string type needs to be created which contains the address of the file location. After that, the file is read line by line in the program. Here, the difference is that the lines are read in a string. They are all joined when string.join is used in the program.

3. Using StreamReader.ReadLine() Method

There is one more way to read the files line by line by StreamReader class. The method is StreamReader.ReadLine(). This runs to the end of the file.

This works as it reads one line of text from the current stream and returns it as a string. This returns the next line from the input stream, or null if the input stream has hit its conclusion. This way it reaches the end of the file.

There is I/O Exception in this too if any I/O error occurs. Another exception is OutOfMemoryException which happens when there is not sufficient memory to create a buffer for the returned string.

Algorithm

The algorithm below will give a step-by-step procedure to read the content of the file line by line using StreamReader.ReadLine() method.

The algorithm discussed below will lead us to create the program.

Step 1  Create a string that will store the name of the file path, remember this is an absolute path.

Step 2 −  A new read is created to read the contents of the file.

Step 3  The while loop runs till the end of the file. The end is determined as it reaches null.

Step 4  Finally the code exits after the contents of the file are read.

Example

Following is the code of the process −

using System;
using System.IO;

public class Example {
   public static void Main() {
      string fileloc = @"D:\ttpt\loc
ew.txt"; using (StreamReader read = new StreamReader(fileloc)) { string line; while ((line = read.ReadLine()) != null) { Console.WriteLine(line); } } } }

Output

This is a file.
Hello from tutorials point.

In this method too firstly a string is created with the file address. Then a read instance is created. It goes to the null i.e., the end of the file by reading the contents line by line.

Time Complexity

Since here we are reading the file line by line but in one go. Irrespective of the methods used i.e., File.ReadLines(), Files.ReadAllLines() and StreamReader.ReadLine(). All of them are reading the file in a single go. So, here the time complexity of every method stands to be O(1).

Conclusion

In this article, we have extensively discussed the C# Program to Read the Content of a File Line by Line. We read how it can be done in three different ways. The two of them from the File class are ReadLines() and ReadAllLines(). The third one is from StreamReader class. Then we discussed the code and algorithm of the three techniques. We hope that this article has helped you enhance your knowledge regarding C#.

Updated on: 21-Apr-2023

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements