Click here to check my latest exciting videos on youtube
Search Mallstuffs

Flag Counter
Spirituality, Knowledge and Entertainment

Locations of visitors to this page

Latest Articles

Move to top
Different types of File Read Operations
Posted By Sarin on Mar 18, 2012     RSS Feeds     Latest Hinduism news

Different types of File Read Operations
In almost all project, there is a need to open a file, read contents of a file, write contents to a file etc. So, Microsoft came up with many convenience classes that make reading and writing text files very easy.  
The following sequence outlines the basic steps necessary to work with text files:
  • Open the file
    • Read/Write to the file
    • Close the file
    Although all these methods will achieve the same functionality more or less, there are considerable differences and hence, you should use the best method as per your requirement.
    So let us explore the various file read methods:

    Reading using File Read operations

    byte[] byt = File.ReadAllBytes("kisna.txt");
    The ReadAllBytes method is actually designed to open up a binary file and read the contents into a byte array. However, it can also be used with normal text file and the text of this file will be converted into a byte array as shown below.

    ReadAllLines method gets the data from the file in a line-by-line fashion and returns a string array

    ReadAllText method read all the contents of a file and stores it into a single string variable.

    Reading using File Streams
    Before we see the example, let us see the basic concepts of stream classes  
    stream <>  is an abstract class that represents a sequence of bytes.
    FileStream <>  derives from Stream and allows you to treat a file as a Stream.
    TextReader <>/TextWriter <>  are abstract classes that allow you to read/write characters.
    StreamReader <>/StreamWriter <> derive from TextReader/TextWriter and provide implementations to read/write from/to a Stream (which can be a FileStream) using an Encoding <>.
    Please choose your options appropriately based on your requirement:
  • For binary data, use Stream
      For text data, use TextWriter/TextReader
    If you start trying to read binary data with TextReader, some discrepancies or inconsistencies might happen

    using TextReader <>
                // create reader & open file
         using (TextReader tr = new StreamReader(filePath))
                    // read a line of text
                    // read full contents of file starting from current position
                    //closing a file

    using StreamReader <>
    rsp160 rsp160 rsp160 // Read data from text file
    using (StreamReader reader = File.OpenText(filePath))
    // reads a single line of contents
    contents = reader.ReadLine();
    // reads complete contents of the the file starting from current position
    contents += reader.ReadToEnd();
    In Example 2, the text file is opened in a manner similar to the method used in Listing 1, except it uses a  StreamReader  class constructor to create an instance of a  TextReader. The StreamReader  class includes additional overloads that allow you to specify the file in different ways, text format encoding, and buffer info.  
    TheTextreader class also includes methods that allow you to invoke the Read() method to read one or more character or use the Peek() method to see what the next character is without pulling it from the stream.  
    rsp160 rsp160 rsp160
    using FileStream

    // Reading contents using FileStream
    using (FileStream stream = File.OpenRead(filePath))
                    // reading data till the end of file            
                    byte[] b = new byte[stream.Length];
                    StringBuilder sb = new StringBuilder();
                    UTF8Encoding temp = new UTF8Encoding(true);
                    while (stream.Read(b, 0, b.Length) > 0)
                    Response.Write("Read File Stream : " + sb.ToString());
    FileStream object gives you extra control over how you want to read or write data from / into a file. This also let you specify the mode of the file to access like in Append mode, or Create mode or only Read mode. Also you can specify Type of access you want to have as third parameter of the File.Open() method.
    FileStream buffers input and output for better performance. In order to use FileStream for reading and writing, you will need to use System.Text namespace as encoding related objects like UTF8Encoding contains in this namespace. 


    Share this to your friends. One of your friend is waiting for your share.
    Related Articles
    Different types of File Read Operations
    Different types of File Write Operations
    Uploading multiple files with drag and drop feature

    Post Comment