Reading and Writing Files using StreamReader in C#


Reading and writing files is an important for any program. C# has provided many classes to perform operations on Files.
Below is the Program showing use of StreamReader and StreamWriter to write the Characters in the Files.

using System;
using System.IO;
using static System.Console;
namespace StreamDemo
{
class Program
{
static string line="";
static void Main(string[] args)
{
StreamWriter st = new StreamWriter("D:\\Test.txt",true);
string [] cars = { "Audi", "Jeep","Jaguar" };
foreach(string s in cars)
{
st.WriteLine(s);
}
st.Close();
StreamReader sr = new StreamReader("D:\\Test.txt");
while ((line = sr.ReadLine()) != null)
{
WriteLine(line);
}
sr.Close(); ReadLine();
}
}
}


Output

Explanation
  • Above Program writes the array of cars in the File character by character.
  • StreamReader and StreamWriter performs the operations on files character by characters.
  • We initialized the object of StreamWriter class and gave the path where the file is to be created.
  • If the files with the same name is already present there at the path, StreamWriter opens that file.
  • If the file at the given path has already some text , and the append property is set to true, the StreamWriter class will append the text at the end of existing text.
  • using foreach loop , we write the array of string is the file.
  • Then we close the connection of the StreamWriter to the file
  • Closing the open connection is necessary , otherwise you will not able to access that file with StreamReader. It will throw an error that file is already in use.
  • Then using the StreamReader object , we access that file.
  • Using While loop we read the containts of the file and display it on the console.


  • Let's do the same program in different way.
    using System;
    using System.IO;
    using static System.Console;
    namespace FileApplication
    {
    class Program
    {
    static void Main(string[] args)
    {
    string[] names = new string[5];
    WriteLine("Enter names of fruits");
    for (int i = 0; i < names.Length; i++)
    {
    names[i] = ReadLine();
    }
    using (StreamWriter sw = new StreamWriter("D:\\names2.txt"))
    {
    WriteLine("Fruit names are below");
    foreach (string s in names)
    {
    sw.WriteLine(s);
    }
    }
    // Read and show each line from the file.
    string line = "";
    using (StreamReader sr = new StreamReader("D:\\names2.txt"))
    {
    while ((line = sr.ReadLine()) != null)
    {
    WriteLine(line);
    }
    }
    ReadKey();
    }
    }
    }


    Output

    Explanation
  • In above program , we are taking user input in the array of strings and writing in the file at the given path.
  • We have not specified if the append is true or false, so by default it is false.
  • Here we have used the statement using so that the connection is automatically close with the close of using statement.
  • the program ask user for name 5 fruits and writes in the file and also displays on the console.
  • Reading and Writing Files using StreamReader in C# Reading and Writing Files using StreamReader in C# Reviewed by LanguageExpert on July 29, 2018 Rating: 5

    No comments