Sunday, August 28, 2016

How to Read a Text File in ASP.NET

In this article I will explain with example how to read a text file.

There are two methods for reading a text file, one using File Class and the other
using StreamReader Class

Using File Class :


The namespace to be included for using File Class is System.IO.
There are two functions named ReadAllText and ReadAllLines of File Class.

ReadAllText opens the file, read all text of that file which is returned as a string 
and then it closes the file.

In case of ReadAllLines, it returns a string array of separate lines of the text file.
It also opens and closes the file by itself.

Below is the sample code :

C# Code :


using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class ReadFile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void btnRead_Click(object sender, EventArgs e)
    {
        string str = File.ReadAllText(@"d:\test.txt");
        Label1.Text = str;

        string[] arr = File.ReadAllLines(@"d:\test.txt");
        foreach (string line in arr)
        {
            if (!String.IsNullOrEmpty(Label2.Text))
                Label2.Text += "\t" + line;
            else
                Label2.Text =line;
        }
        
    }
}


Using StreamReader Class :


StreamReader Class is also contained in the namespace System.IO. StreamReader 
Class reads characters from a byte stream in a particular encoding.

Methods used in StreamReader class are ReadLine and ReadToEnd

With StreamReader, files can be read line by line and so it consumes less memory
compared to File.ReadAllLines. StreamReader is efficient in case of bigger files
but in other case, using File class is simple.

C# Code :


using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class ReadFile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
   protected void btnStream_Click(object sender, EventArgs e)
   {
        // using is used as it take care of object disposal
        using (StreamReader sr = new StreamReader(@"d:\test.txt"))
        {
              // Reads line of characters from the current stream and returns as a string
              Label1.Text = sr.ReadLine();

        }
   }
}

In this case it will display the first line of the text file. In order to read all lines,
code is as following :

 protected void btnStream_Click(object sender, EventArgs e)
   {
        using (StreamReader sr = new StreamReader(@"d:\test.txt"))
        {
             string line;
             while ((line = sr.ReadLine()) != null)
            {
                if (!String.IsNullOrEmpty(Label1.Text))
                    Label1.Text += "\t" + line;
                else
                    Label1.Text = line;
            }

        }
   }

In the case if user wants to read till the end, the code is as following:

protected void btnStream_Click(object sender, EventArgs e)
 {
        using (StreamReader sr = new StreamReader(@"d:\test.txt"))
        {
             //  Reads the stream from the current position till the end and return as a string
             Label1.Text = sr.ReadToEnd();

        }
   }

No comments:

Post a Comment