Which way is best to read from a file?

Discussion in 'ASP .Net' started by ESPN Lover, Oct 12, 2004.

  1. ESPN Lover

    ESPN Lover Guest

    Below is two snippets of code from MSDN showing how to read a file. Is one
    way preferred over the other and why? Thanks.


    using System;
    using System.IO;

    class Test
    {
    public static void Main()
    {
    try
    {
    // Create an instance of StreamReader to read from a file.
    // The using statement also closes the StreamReader.
    using (StreamReader sr = new StreamReader("TestFile.txt"))
    {
    String line;
    // Read and display lines from the file until the end of
    // the file is reached.
    while ((line = sr.ReadLine()) != null)
    {
    Console.WriteLine(line);
    }
    }
    }
    catch (Exception e)
    {
    // Let the user know what went wrong.
    Console.WriteLine("The file could not be read:");
    Console.WriteLine(e.Message);
    }
    }
    }



    using System;
    using System.IO;
    using System.Text;

    class Test
    {
    public static void Main()
    {
    string path = @"c:\temp\MyTest.txt";

    // Open the stream and read it
    using (FileStream fs = File.OpenRead(path))
    {
    byte[] b = new byte[1024];
    UTF8Encoding temp = new UTF8Encoding(true);

    while (fs.Read(b,0,b.Length) > 0)
    {
    Console.WriteLine(temp.GetString(b));
    }
    }
    }
    }
     
    ESPN Lover, Oct 12, 2004
    #1
    1. Advertising

  2. Hi,

    Both ways are useful depending on what you need to accomplish. The top way
    reads the file one line at a time meaning it reads till the line break. If
    your file contains lists of names:

    Bill
    Smith
    Mark
    Wilson
    Brian
    Winters

    Then reading line by line will work for you. However if your file is
    created without using line breaks and instead has fixed length fields, then
    the bottom way will let you grab these anywhere in the file instead of
    having to iterate through all the lines.

    Bill456Smith56789Mark456Wilson6789Brian56Winters789

    The line above shows a fixed length field file (visualize a space where each
    number is). Each first name takes up 7 characters, each last name takes up
    10. So if you wanted to get the 3rd first name you would read 7 characters
    starting at position 34 (remember streams start at 0). Good luck! Ken.

    --
    Ken Dopierala Jr.
    For great ASP.Net web hosting try:
    http://www.webhost4life.com/default.asp?refid=Spinlight
    If you sign up under me and need help, email me.

    "ESPN Lover" <> wrote in message
    news:...
    > Below is two snippets of code from MSDN showing how to read a file. Is one
    > way preferred over the other and why? Thanks.
    >
    >
    > using System;
    > using System.IO;
    >
    > class Test
    > {
    > public static void Main()
    > {
    > try
    > {
    > // Create an instance of StreamReader to read from a file.
    > // The using statement also closes the StreamReader.
    > using (StreamReader sr = new StreamReader("TestFile.txt"))
    > {
    > String line;
    > // Read and display lines from the file until the end of
    > // the file is reached.
    > while ((line = sr.ReadLine()) != null)
    > {
    > Console.WriteLine(line);
    > }
    > }
    > }
    > catch (Exception e)
    > {
    > // Let the user know what went wrong.
    > Console.WriteLine("The file could not be read:");
    > Console.WriteLine(e.Message);
    > }
    > }
    > }
    >
    >
    >
    > using System;
    > using System.IO;
    > using System.Text;
    >
    > class Test
    > {
    > public static void Main()
    > {
    > string path = @"c:\temp\MyTest.txt";
    >
    > // Open the stream and read it
    > using (FileStream fs = File.OpenRead(path))
    > {
    > byte[] b = new byte[1024];
    > UTF8Encoding temp = new UTF8Encoding(true);
    >
    > while (fs.Read(b,0,b.Length) > 0)
    > {
    > Console.WriteLine(temp.GetString(b));
    > }
    > }
    > }
    > }
    >
    >
     
    Ken Dopierala Jr., Oct 12, 2004
    #2
    1. Advertising

  3. ESPN Lover

    ESPN Lover Guest

    All I'm trying to do is copy the contents of a file (actually an HTML page
    on the file system) into a string. Process the string and output a new
    file. So the idea is to read, upfront, the entire file's contents and place
    it into a string. This is manipulated and then I'll write out a new file.

    "Ken Dopierala Jr." <> wrote in message
    news:eV%...
    > Hi,
    >
    > Both ways are useful depending on what you need to accomplish. The top

    way
    > reads the file one line at a time meaning it reads till the line break.

    If
    > your file contains lists of names:
    >
    > Bill
    > Smith
    > Mark
    > Wilson
    > Brian
    > Winters
    >
    > Then reading line by line will work for you. However if your file is
    > created without using line breaks and instead has fixed length fields,

    then
    > the bottom way will let you grab these anywhere in the file instead of
    > having to iterate through all the lines.
    >
    > Bill456Smith56789Mark456Wilson6789Brian56Winters789
    >
    > The line above shows a fixed length field file (visualize a space where

    each
    > number is). Each first name takes up 7 characters, each last name takes

    up
    > 10. So if you wanted to get the 3rd first name you would read 7

    characters
    > starting at position 34 (remember streams start at 0). Good luck! Ken.
    >
    > --
    > Ken Dopierala Jr.
    > For great ASP.Net web hosting try:
    > http://www.webhost4life.com/default.asp?refid=Spinlight
    > If you sign up under me and need help, email me.
    >
    > "ESPN Lover" <> wrote in message
    > news:...
    > > Below is two snippets of code from MSDN showing how to read a file. Is

    one
    > > way preferred over the other and why? Thanks.
    > >
    > >
    > > using System;
    > > using System.IO;
    > >
    > > class Test
    > > {
    > > public static void Main()
    > > {
    > > try
    > > {
    > > // Create an instance of StreamReader to read from a file.
    > > // The using statement also closes the StreamReader.
    > > using (StreamReader sr = new StreamReader("TestFile.txt"))
    > > {
    > > String line;
    > > // Read and display lines from the file until the end of
    > > // the file is reached.
    > > while ((line = sr.ReadLine()) != null)
    > > {
    > > Console.WriteLine(line);
    > > }
    > > }
    > > }
    > > catch (Exception e)
    > > {
    > > // Let the user know what went wrong.
    > > Console.WriteLine("The file could not be read:");
    > > Console.WriteLine(e.Message);
    > > }
    > > }
    > > }
    > >
    > >
    > >
    > > using System;
    > > using System.IO;
    > > using System.Text;
    > >
    > > class Test
    > > {
    > > public static void Main()
    > > {
    > > string path = @"c:\temp\MyTest.txt";
    > >
    > > // Open the stream and read it
    > > using (FileStream fs = File.OpenRead(path))
    > > {
    > > byte[] b = new byte[1024];
    > > UTF8Encoding temp = new UTF8Encoding(true);
    > >
    > > while (fs.Read(b,0,b.Length) > 0)
    > > {
    > > Console.WriteLine(temp.GetString(b));
    > > }
    > > }
    > > }
    > > }
    > >
    > >

    >
    >
     
    ESPN Lover, Oct 13, 2004
    #3
  4. Hi,

    Then all you have to do is:

    sr.BaseStream.Seek(0, SeekOrigin.Begin);
    strEntireFile = sr.ReadToEnd();

    Put that in the top version of the examples you showed in the place of the
    while loop. Good luck! Ken.

    --
    Ken Dopierala Jr.
    For great ASP.Net web hosting try:
    http://www.webhost4life.com/default.asp?refid=Spinlight
    If you sign up under me and need help, email me.

    "ESPN Lover" <> wrote in message
    news:...
    > All I'm trying to do is copy the contents of a file (actually an HTML page
    > on the file system) into a string. Process the string and output a new
    > file. So the idea is to read, upfront, the entire file's contents and

    place
    > it into a string. This is manipulated and then I'll write out a new file.
    >
    > "Ken Dopierala Jr." <> wrote in message
    > news:eV%...
    > > Hi,
    > >
    > > Both ways are useful depending on what you need to accomplish. The top

    > way
    > > reads the file one line at a time meaning it reads till the line break.

    > If
    > > your file contains lists of names:
    > >
    > > Bill
    > > Smith
    > > Mark
    > > Wilson
    > > Brian
    > > Winters
    > >
    > > Then reading line by line will work for you. However if your file is
    > > created without using line breaks and instead has fixed length fields,

    > then
    > > the bottom way will let you grab these anywhere in the file instead of
    > > having to iterate through all the lines.
    > >
    > > Bill456Smith56789Mark456Wilson6789Brian56Winters789
    > >
    > > The line above shows a fixed length field file (visualize a space where

    > each
    > > number is). Each first name takes up 7 characters, each last name takes

    > up
    > > 10. So if you wanted to get the 3rd first name you would read 7

    > characters
    > > starting at position 34 (remember streams start at 0). Good luck! Ken.
    > >
    > > --
    > > Ken Dopierala Jr.
    > > For great ASP.Net web hosting try:
    > > http://www.webhost4life.com/default.asp?refid=Spinlight
    > > If you sign up under me and need help, email me.
    > >
    > > "ESPN Lover" <> wrote in message
    > > news:...
    > > > Below is two snippets of code from MSDN showing how to read a file. Is

    > one
    > > > way preferred over the other and why? Thanks.
    > > >
    > > >
    > > > using System;
    > > > using System.IO;
    > > >
    > > > class Test
    > > > {
    > > > public static void Main()
    > > > {
    > > > try
    > > > {
    > > > // Create an instance of StreamReader to read from a file.
    > > > // The using statement also closes the StreamReader.
    > > > using (StreamReader sr = new StreamReader("TestFile.txt"))
    > > > {
    > > > String line;
    > > > // Read and display lines from the file until the end

    of
    > > > // the file is reached.
    > > > while ((line = sr.ReadLine()) != null)
    > > > {
    > > > Console.WriteLine(line);
    > > > }
    > > > }
    > > > }
    > > > catch (Exception e)
    > > > {
    > > > // Let the user know what went wrong.
    > > > Console.WriteLine("The file could not be read:");
    > > > Console.WriteLine(e.Message);
    > > > }
    > > > }
    > > > }
    > > >
    > > >
    > > >
    > > > using System;
    > > > using System.IO;
    > > > using System.Text;
    > > >
    > > > class Test
    > > > {
    > > > public static void Main()
    > > > {
    > > > string path = @"c:\temp\MyTest.txt";
    > > >
    > > > // Open the stream and read it
    > > > using (FileStream fs = File.OpenRead(path))
    > > > {
    > > > byte[] b = new byte[1024];
    > > > UTF8Encoding temp = new UTF8Encoding(true);
    > > >
    > > > while (fs.Read(b,0,b.Length) > 0)
    > > > {
    > > > Console.WriteLine(temp.GetString(b));
    > > > }
    > > > }
    > > > }
    > > > }
    > > >
    > > >

    > >
    > >

    >
    >
     
    Ken Dopierala Jr., Oct 13, 2004
    #4
  5. ESPN Lover

    ESPN Lover Guest

    Thanks, Ken!! That's what I was after :)


    "Ken Dopierala Jr." <> wrote in message
    news:%...
    > Hi,
    >
    > Then all you have to do is:
    >
    > sr.BaseStream.Seek(0, SeekOrigin.Begin);
    > strEntireFile = sr.ReadToEnd();
    >
    > Put that in the top version of the examples you showed in the place of the
    > while loop. Good luck! Ken.
     
    ESPN Lover, Oct 13, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alan Silver
    Replies:
    8
    Views:
    3,934
    Scott Allen
    Dec 8, 2005
  2. Karthikesh Raju

    best way to read a configuration file

    Karthikesh Raju, Mar 3, 2004, in forum: Python
    Replies:
    5
    Views:
    449
    Kevin Dahlhausen
    Mar 3, 2004
  3. Replies:
    4
    Views:
    376
  4. Bret Schuhmacher

    Best way to read an XML properties file?

    Bret Schuhmacher, Jul 10, 2007, in forum: Java
    Replies:
    1
    Views:
    674
    Stefan Ram
    Jul 10, 2007
  5. Eddy Xu
    Replies:
    5
    Views:
    127
    Eddy Xu
    Apr 11, 2008
Loading...

Share This Page