Monday, February 25, 2013

Reading from Text File (C#)

 

Sample code to read data from text file : use “System.IO” namespace to work with file Input Output Operations

Aspx Code :

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SaveTextToFile.aspx.cs" Inherits="Santhakam_SaveTextToFile" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>READING TEXT FILE</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>

              <tr>
            <td colspan="2">
                <asp:Button ID="btnReadFile" runat="server" Text="ReadFile" OnClick="btnReadFile_Click" />
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:Label ID="lblContents" runat="server" Font-Bold="True" ForeColor="Red"></asp:Label>
                <asp:Label ID="lblFileContents" runat="server" Font-Bold="True" ForeColor="Black"></asp:Label>
            </td>
        </tr>
    </table>
    </div>
    </form>
</body>
</html>

Code Behind :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

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

    }
    protected String ReadTextFile()
    {
        String FileContents = string.Empty;
        StreamReader ObjReader;
        try
        {
            ObjReader = File.OpenText("C:\\Test.txt");
            FileContents = ObjReader.ReadToEnd();
            return FileContents;

        }
        catch(Exception ex)
        {
            return "Error";
        }
    }
   protected void btnReadFile_Click(object sender, EventArgs e)
    {
        lblContents.Text = "File Contents : ";
        lblFileContents.Text = ReadTextFile();
    }
}

Output:

ReadTextFile1         ReadTextFile2

2 comments:

  1. Here's a blog which benchmarks 9 common techniques to determine the fastest way to read text files in C# .Net.

    Definitely worth a look for those interested in the fastest way:

    http://blogs.davelozinski.com/curiousconsultant/csharp-net-fastest-way-to-read-text-files

    _

    ReplyDelete
  2. Well, there have been many benchmarks. This blog article uses code to demonstrate that the fastest way to read a text file is with the age old method:

    using (StreamReader sr = File.OpenText(fileName))
    {
    string s = String.Empty;
    while ((s = sr.ReadLine()) != null)
    {
    //we’re just testing read speeds
    }
    }

    Reference:
    http://blogs.davelozinski.com/curiousconsultant/csharp-net-fastest-way-to-read-text-files

    HOWEVER, if you need to lots of processing, this article benchmarks the fastest way to both read and process a text file. Basically, implementing parallel processing. Here’s the basic code snippet:

    AllLines = new string[MAX]; //only allocate memory here
    using (StreamReader sr = File.OpenText(fileName))
    {
    int x = 0;
    while (!sr.EndOfStream)
    {
    AllLines[x] = sr.ReadLine();
    x += 1;
    }
    } //CLOSE THE FILE because we are now DONE with it.
    Parallel.For(0, AllLines.Length, x =>
    {
    DoStuff(AllLines[x]);
    });

    Reference:
    http://blogs.davelozinski.com/curiousconsultant/the-fastest-way-to-read-and-process-text-files


    _

    ReplyDelete