codecandle logo




 

C# - IO - StreamReader - Reading Data from a File (with using StreamReader Class instance).


Related: C# Tutorial

Date: 30.08.2011

In ADO.NET, in order to read a text or data from a file as basic method: StreamReader Class can be used. StreamWriter Class instance method supports two main method to read an input from the file.

1. Read(): It is used to read a text without line break break into the file. Paragraph texts can be used to be read with that method.
2. ReadLine(): To reada text with line break: line by line from the file.

Example demonstrates following logical steps for how to read read a text or data from a File using StreamReader Class Instance in C#.

1. Creates a FileStream Class instance in memory for input stream, and creates a memory work area as buffer for file that is passed at parameter as created.
2. Creates a StreamReader Class instance and initializes from FileStream Class instance.
3. Reads the text or data from the file with ReadLine method() line by line.

Reading text from a File using StreamReader Class instance.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.IO; //hosts the StreamReader Class

namespace ConsoleApplication3
{
class Program
{
static void Main(string [] args)
{
//declare a variable to store input stream content
string v_str_readedLine;

try
{
//opening file and creating a input stream for file resource
//create a filestream Class instance and create file there
FileStream o_fs_aFileStream = new FileStream(@"c:\tmp\2.txt", FileMode.OpenOrCreate);

//create a streamreader Class instance for reading input stream buffered
StreamReader o_streamReader_aReader = new StreamReader(o_fs_aFileStream);

//read the first line from input stream which is connected to file
v_str_readedLine = o_streamReader_aReader.ReadLine();

//loop till next readline returns null (indicates end of file to end the loop)
while (v_str_readedLine != null)
{
//display the text which is read
Console.WriteLine(v_str_readedLine);

//assign to String variable the next text from input stream read
v_str_readedLine = o_streamReader_aReader.ReadLine();

Console.WriteLine("a new line from file is read...");


}
//close streamreader
o_streamReader_aReader.Close();
}
//handles exception on error
catch (System.Exception o_aException)
{
Console.WriteLine("exception is fired");
Console.WriteLine(o_aException.ToString());

}

//preventing console window to be closed after executing application code
Console.Read();
}
}
}





Following image displays the file screen-shot which has been created for example application to use above example.

C# - IO - StreamReader - Reading Data from a File (with using StreamReader Class instance).

Following image displays the file screen-shot of application console output.

C# - IO - StreamReader - Reading Data from a File (with using StreamReader Class instance).



Area: articles

Reference resources: unknown node
Tags: C# - IO - StreamReader - Reading a data or text from a File

Catagory layer 1: Programming
Catagory layer 2: Languages

Code layer 1: Languages
Code layer 2: csharp
Code layer 3: C# 1.0
Code layer 4: io
Code layer 5: streamreader-reading-data-from-a-file-(with-using-streamreader-class-instance)

Reader layer 1: beginner

Sector layer 1: C# programmer

Fuzzy layer 1: how to read data from a file in C#
Fuzzy layer 2: how to read a text into a vaiable with using StreamReader Class instance in C#

Nodes: Languages\C# (1)
  • comment icon
  • Comments Section
 
  • 26.02.2014 11:46:33 post by Alias Name

kkkkk

  • 07.03.2013 12:40:25 post by Alias Name

Comment...

  • Post your Comment:


Chat section


[ Copy this | Start New | Full Size ]

CodeCandle

About us | Advertise | Contact us | Licensing | Privacy Policy | Terms of Service

© 2010 CodeCandle. All rights reserved.