w3resource

C#: Append some text to an existing file

C# Sharp File Handling : Exercise-8 with Solution

Write a program in C# Sharp to append some text to an existing 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 filexercise3 // Declaring a class named filexercise3
{
    public static void Main() // Declaring the Main method
    {
        string fileName = @"mytest.txt"; // Initializing a string variable with the file name

        try // Starting a try block to catch exceptions
        {
            // Delete the file if it exists.
            if (File.Exists(fileName)) // Checking if the file exists
            {
                File.Delete(fileName); // If the file exists, delete it
            }
			
            // Displaying a message to append some text to an existing file
            Console.Write("\n\n Append some text to an existing file  :\n");
            Console.Write("--------------------------------------------\n");

            // Create the file and write initial content to it.
            using (StreamWriter fileStr = File.CreateText(fileName)) // Creating a StreamWriter to write text to the file
            {
                // Writing initial content to the file
                fileStr.WriteLine(" Hello and Welcome");
                fileStr.WriteLine(" It is the first content");
                fileStr.WriteLine(" of the text file mytest.txt");
            }

            // Reading and displaying the content of the file before appending
            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(""); // Adding a newline for formatting
            }

            // Appending a line at the end of the file
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"mytest.txt", true)) // Appending to the existing file using StreamWriter
            {
                file.WriteLine(" This is the line appended at the last line."); // Appending a line to the file
            }

            // Reading and displaying the content of the file after appending
            using (StreamReader sr = File.OpenText(fileName)) // Opening a StreamReader again to read the updated content of the file
            {
                string s = "";
                Console.WriteLine(" Here is the content of the file after appending the text : ");
                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(""); // Adding a newline for formatting
            }
        }
        catch (Exception MyExcep) // Catching any exceptions that might occur
        {
            Console.WriteLine(MyExcep.ToString()); // Displaying the exception details if any
        }
    }
}

Sample Output:

Append some text to an existing file  :                                                                      
--------------------------------------------                                                                  
 Here is the content of the file mytest.txt :                                                                 
 Hello and Welcome                                                                                            
 It is the first content                                                                                      
 of the text file mytest.txt                                                                                  
     
 Here is the content of the file after appending the text :                                                   
 Hello and Welcome                                                                                            
 It is the first content                                                                                      
 of the text file mytest.txt                                                                                  
 This is the line appended at last line. 
 

Flowchart :

Flowchart: C# Sharp Exercises - Append some text to an existing file.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a program in C# Sharp to create and write some line of text into a file which does not contain a given string in a line.
Next: Write a program in C# Sharp to create and copy the file to another name and display the content.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

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-8.php