C#: Read the first line from a file
C# Sharp File Handling: Exercise-11 with Solution
Write a C# Sharp program to read the first line of a file.
Sample Solution:-
C# Sharp Code:
using System; // Importing the System namespace for basic functionalities
using System.IO; // Importing the System.IO namespace for file operations
using System.Text; // Importing the System.Text namespace for encoding
class filexercise11 // Declaring a class named filexercise11
{
public static void Main() // Declaring the Main method
{
string fileName = @"mytest.txt"; // Initializing a string variable with the file name
try // Starting a try-catch block to handle exceptions
{
// Delete the file if it exists.
if (File.Exists(fileName)) // Checking if the file exists and deleting it if true
{
File.Delete(fileName); // Deleting the file if it exists
}
// Displaying a message to read the first line from a file
Console.Write("\n\n Read the first line from a file :\n");
Console.Write("---------------------------------------\n");
// Create the file and write some lines of text to it.
using (StreamWriter fileStr = File.CreateText(fileName)) // Creating a StreamWriter to write text to the file
{
fileStr.WriteLine(" test line 1"); // Writing line 1 to the file
fileStr.WriteLine(" test line 2"); // Writing line 2 to the file
fileStr.WriteLine(" Test line 3"); // Writing line 3 to the file
}
// Displaying the content of the file
using (StreamReader sr = File.OpenText(fileName)) // Opening a StreamReader to read the content of the file
{
string s = "";
Console.WriteLine(" Here is the content of the file mytest.txt : ");
while ((s = sr.ReadLine()) != null) // Looping through each line until the end of the file
{
Console.WriteLine(s); // Displaying each line in the console
}
Console.WriteLine("");
}
// Displaying the content of the first line of the file
Console.Write("\n The content of the first line of the file is :\n");
if (File.Exists(fileName)) // Checking if the file exists
{
string[] lines = File.ReadAllLines(fileName); // Reading all lines from the file
Console.Write(lines[0]); // Writing the first line of the file to the console
}
Console.WriteLine();
}
catch (Exception MyExcep) // Catching and handling exceptions
{
Console.WriteLine(MyExcep.ToString()); // Displaying exception details in case of error
}
}
}
Sample Output:
Read the first line from a file : --------------------------------------- Here is the content of the file mytest.txt : test line 1 test line 2 Test line 3 The content of the first line of the file is : test line 1
Flowchart :
C# Sharp Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Write a program in C# Sharp to create a file and move the file into the same directory to another name.
Next: Write a program in C# Sharp to create and read the last line of a file.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/csharp-exercises/file-handling/csharp-file-handling-exercise-11.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics