Skip to content Skip to sidebar Skip to footer

C Read a File Line by Line

C# Read a Text File Line by Line

  1. Read a Text File Line by Line past Using File.ReadLines() Method in C
  2. Read a Text File Line by Line past Using File.ReadAllLines() Method in C
  3. Read a Text File Line past Line by Using StreamReader.ReadLine() Method in C

Nosotros can perform several operations on a text file. To use this data in any program, we first need to read that information in a proper data construction.

In C#, there are several ways to read a text file line by line efficiently. Continue reading below to detect these means.

Read a Text File Line past Line by Using File.ReadLines() Method in C

File.ReadLines() method is the best method establish to read a text file line past line efficiently. This method returns an Enumerable for large text files, that'south why we have created an Enumerable cord object to store the text file.

The correct syntax to use this method is equally follows:

              File.ReadLines(FileName);                          

Case Code:

              using Organisation; using Organisation.Collections.Generic; using Organisation.IO;  public class ReadFile {     public static void Principal()     {       string FileToRead = @"D:\New folder\textfile.txt";       // Creating enumerable object         IEnumerable<cord> line = File.ReadLines(FileToRead);       Console.WriteLine(String.Join(Surround.NewLine, line));        } }                          

Output:

              // All the text, the file contains will brandish here.                          

File.ReadLines() method throws an IOException if there is an issue opening the file and FileNotFoundException if the requested file does not exist.

Read a Text File Line past Line by Using File.ReadAllLines() Method in C

File.ReadAllLines() method can as well be used to read a file line by line. It does not return an Enumerable but returns a cord array that contains all the lines of the text file.

The correct syntax to use this method is as follows:

              File.ReadAllLines(FileName);                          

Example Code:

              using Organisation; using Arrangement.IO;  public grade ReadFile {     public static void Master()     {       string FileToRead = @"D:\New folder\textfile.txt";       // Creating string assortment         string[] lines = File.ReadAllLines(FileToRead);       Console.WriteLine(String.Bring together(Environment.NewLine, lines));        } }                          

Output:

              // All the text, the file contains will brandish here.                          

This method also throws exceptions simply like the File.ReadLines() method. These exceptions are then handled using a endeavour-catch cake.

Read a Text File Line by Line past Using StreamReader.ReadLine() Method in C

The StreamReader class in C# provides a method StreamReader.ReadLine(). This method reads a text file to the end line by line.

The correct syntax to employ this method is every bit follows:

              //Nosotros accept to create Streader Object to employ this method StreamReader ObjectName = new StreamReader(FileName); ObjectName.ReadLine();                          

Example Code:

              using Arrangement; using System.IO;  public class ReadFile {     public static void Master()     {       string FileToRead = @"D:\New folder\textfile.txt";       using (StreamReader ReaderObject = new StreamReader(FileToRead))       {           cord Line;           // ReaderObject reads a single line, stores it in Line cord variable and then displays it on panel           while((Line = ReaderObject.ReadLine()) != null)           {               Panel.WriteLine(Line);           }       }              } }                          

Output:

              // All the text, the file contains will display here.                          

Write for us

DelftStack articles are written past software geeks similar you. If you also would similar to contribute to DelftStack by writing paid articles, you lot can check the write for us page.

Related Article - Csharp File

  • Suspend to Text File in C#
  • Write a Stream to a File in C#
  • Ezoic

    bondhadjembals70.blogspot.com

    Source: https://www.delftstack.com/howto/csharp/how-to-read-a-text-file-line-by-line-in-csharp/

    Publicar un comentario for "C Read a File Line by Line"