Skip to content
Related Articles

Related Articles

File.ReadLines(String, Encoding) Method in C# with Examples
  • Last Updated : 01 Jun, 2020

File.ReadLines(String, Encoding) is an inbuilt File class method that is used to read the lines of a file that has a specified encoding.

Syntax:

public static System.Collections.Generic.IEnumerable ReadLines (string path, System.Text.Encoding encoding);

Parameter: This function accepts two parameters which are illustrated below:

  • path: This is the specified file for reading.
  • encoding: This encoding is applied to the contents of the file.

Exceptions:



  • ArgumentException: The path is a zero-length string, contains only white space, or one or more invalid characters defined by the GetInvalidPathChars() method.
  • ArgumentNullException: The path is null.
  • DirectoryNotFoundException: The path is invalid.
  • FileNotFoundException: The file specified by the path was not found.
  • IOException: An I/O error occurred while opening the file.
  • PathTooLongException: The path exceeds the system-defined maximum length.
  • SecurityException: The caller does not have the required permission.
  • UnauthorizedAccessException: The path specifies a file that is read-only. OR this operation is not supported on the current platform. OR the path is a directory. OR the caller does not have the required permission.

Return Value: Returns all the lines of the specified file.

Below are the programs to illustrate the File.ReadLines(String, Encoding) method.

Program 1: Initially, a file file.txt is created with some contents shown below-

file.txt




// C# program to illustrate the usage
// of File.ReadLines(String, Encoding) method
  
// Using System, System.IO
// and System.Text namespaces
using System;
using System.IO;
using System.Text;
  
public class GFG {
    public static void Main(String[] argv)
    {
        // Calling the ReadLines(String, Encoding) function
        foreach(string line in File.ReadLines(@"file.txt", Encoding.UTF8))
        {
            // Printing the file contents
            Console.WriteLine(line);
        }
    }
}

Output:

GFG
gfg
Geeks
GeeksforGeeks
geeksforgeeks

Program 2: Initially, a file file.txt is created with some contents shown below-

file.txt

Below code filter some contents from the file and prints them back.




// C# program to illustrate the usage
// of File.ReadLines(String, Encoding) method
  
// Using System, System.IO
// and System.Text namespaces
using System;
using System.IO;
using System.Text;
  
public class GFG {
    public static void Main(String[] argv)
    {
        // Calling the ReadLines(String, Encoding) function
        foreach(string line in File.ReadLines(@"file.txt", Encoding.UTF8))
        {
            // Filtering the file contents and printing
            if (line.Contains("GFG")) {
                Console.WriteLine(line);
            }
        }
    }
}

Output:

GFG

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :