How to use StreamReader and streamWriter in reading and writing a file C#


Today i want discuss an article how to read and write a file by using StreamWriter and StreamReader ? using C# with code snippets and real time images.This article will explain only some of the Main Methods that are associate with these two classes.

Many programmers will come across a situation that they need to work with text files.So how to use StreamWriter and StreamReader to create,write and read the Text File that i will discuss in this Article.What are the Main methods that are associated with this two classes(StreamWriter and StreamReader) how to use them in this Scenario(i.e reading and writing in Text file).

The Objective of StreamWriter is to create a new file and write the contents on that file.The Objective of StreamReader is to read those file contents.

Before Going to Use them the StreamWriter and StreamReader they are Under the Namespace of System.IO

if you are Using VB.NET

imports System.IO

if you are a c# Developer use

Using System.IO;

When you are Using StreamWriter and streamReader you need to remember Four main Methods those are

1)Read()
2)Write()
3)Close() and
4)Dispose()

Apart form these You can also ReadLine(), writeLine(), ReadAllines() and ReadtoEnd()

Both the Class Constructors will take path as an an argument in other words we can call them as parameter Constructor

Look at the Below Example of code you came to know what i described there.



StreamWriter sw = new StreamWriter(path of the File name);
StreamReader sr =new StreamReader(Path of the File name);



you can pass Other Arguments in this Constructor.I had described Only one main Argument here.

In this above code i am passing a file path in both StreamReader and StreamWriter Class.

As earlier discuss in the above how to Use those Methods in Programming i will discuss in below with snippet of Code lines

Using of streamwriter class

using the above code of line we have passed an argument(i.e. path of the file name) in streamWriter constructor.without doing any file Operations StreamWriter class will create a new file for us and data will be written in that Text file.

New text file created

when you open this text file you see all the Data written to it.

Data written in it

I want to read all the text which is written in TextFile or notepad.with using of StreamReader we will see how we will do it.

streamreader reading the contents of text file

You have to remember two more Methods when your using StreamWriter and StreamReader i.e Close() and Dispose() Methods.Without Using these Methods your StreamReader and StreamWriter will never Work.When you want to work them in Continuous Manner(i.e they are in loop).Even if you are not using them in loop. For efficient Programing you need to use this Methods Dispose(),Close() Methods.

In Finally block You use these methods the code is as follows.


finally
{
sw.Close(); //close the streamwriter
sw.Dispose();
sr.Close();
sr.Dispose(); // Release the Resources from the Memory when you want again use that you need to instantiate the Object using new key word.
}



The whole code is written below


StreamWriter sw =null;
StreamReader sr=null;
string strresponse =string.Empty;
string strnew=string.Empty;
ArrayList arrlist =null;
WebClient wcMicrosoft =new WebClient();
System.Text.UTF8Encoding ObjUtF8Encoding =new System.Text.UTF8Encoding();
try
{
strresponse = ObjUtF8Encoding.GetString(wcMicrosoft.DownloadData("http://scores.sify.com/index.shtml"));
}
catch(Exception)
{


}
try
{
sw = new StreamWriter("D:\\Cricket.txt");
sw.WriteLine(strresponse);
sw.Close();


sr = new StreamReader("D:\\Cricket.txt");
string line = sr.ReadToEnd();
arrlist =new ArrayList();
arrlist.Insert(0,line.ToString());

sr.Close();
}
catch(Exception ex)
{
Response.Write(ex.Message);

if(sr!=null)
{
sr.Close();
}
if(sw !=null)
{
sw.Close();
}

}
finally
{
if(sr!=null)
{
sr.Close();
sr.Dispose();
}
if(sw!=null)
{
sw.Close();
sw.Dispose();
}

}



// Put user code to initialize the page here


}





Attachments

  • Using of streamwriter class (45555-212411-Using-of-streamwriter-class.bmp)
  • Data written in it (45555-214733-Data-written-in-it.bmp)
  • Article by srirama
    A Good advice from parent to a Child , Master to a Student , Scholar to an Ignorant is like a doctor prescribed pill it is bitter to take but when they take it will do all good for them --- Bhushan

    Follow srirama or read 73 articles authored by srirama

    Comments

    No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: