10 Replies - 18473 Views - Last Post: 14 November 2013 - 10:28 AM Rate Topic: -----

#1 vivek20055   User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 82
  • Joined: 02-November 13

Difference between File.ReadAllLines and StreamReader

Posted 12 November 2013 - 04:57 AM

Hi,

I wrote a console program to read a file using both File.ReadAllLines and StreamReader

StreamReader gives gives me the content of the file on console

But File.ReadAllLines gives me System.String[]

What is the difference between them

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] fileContents = null;
            //StreamReader fileContents = new StreamReader(@"\\global.scd.scania.com\home\se\121\valhbc\Desktop\log files\1302_P3\Red_SOP1302_P3.xls");
            fileContents = File.ReadAllLines(@"\\global.scd.scania.com\home\se\121\valhbc\Desktop\log files\1302_P3\Red_SOP1302_P3.xls");    
            //Console.WriteLine(fileContents.ReadToEnd());
                Console.WriteLine(fileContents);
                for (double i = 0; i <= 1000000000000; i++)
                {
                }
            }
        }
    }


Where should I use File.ReadAllLines and where should I use StreamReader

Regards
John

Is This A Good Question/Topic? 0
  • +

Replies To: Difference between File.ReadAllLines and StreamReader

#2 Michael26   User is offline

  • Futurama: Insert funny joke here
  • member icon

Reputation: 414
  • View blog
  • Posts: 1,664
  • Joined: 08-April 09

Re: Difference between File.ReadAllLines and StreamReader

Posted 12 November 2013 - 05:00 AM

Yes, there's a difference. You'll find out when you try to read a file that's a gigabyte or more. That's a guaranteed crash on the 32-bit version. No such problem with a StreamReader that reads line-by-line, it will use very little memory. It depends on what the rest of your program does but try to limit the convenience method to files no larger than, say, a couple of megabytes.

So, ReadAllLines will read and store everything to string[], if that array is larger than int.maxvalue then that will produce memory overflow(limit of 32bit OS).
The StreamReader read line by line which is far more efficient because you are working with portion of the file that is loaded in memory.

Reading benchmark
Posted Image

Also i found this msdn article

This post has been edited by Michael26: 12 November 2013 - 05:19 AM

Was This Post Helpful? 1
  • +
  • -

#3 vivek20055   User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 82
  • Joined: 02-November 13

Re: Difference between File.ReadAllLines and StreamReader

Posted 12 November 2013 - 05:23 AM

Hi,

Thanks for your reply. Ok, now I understand about the difference.

Why I cannot see the data of the file by using Console.Writeline();???

when I am using File.ReadAllLines.

It is just showing System.String[]

It means the data of the file is stored in string?

Is there any other way that I can see the data on console?

Thanks
John
Was This Post Helpful? 0
  • +
  • -

#4 Michael26   User is offline

  • Futurama: Insert funny joke here
  • member icon

Reputation: 414
  • View blog
  • Posts: 1,664
  • Joined: 08-April 09

Re: Difference between File.ReadAllLines and StreamReader

Posted 12 November 2013 - 05:32 AM

Because ReadAllLines returns array, and to print array using WriteLine() you need to access individual elements, like Console.WriteLine(array[0]); this will print out first element. And if you want to print all elements in ReadAllLines you need a for loop.

This post has been edited by Michael26: 12 November 2013 - 05:33 AM

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6829
  • View blog
  • Posts: 28,311
  • Joined: 12-December 12

Re: Difference between File.ReadAllLines and StreamReader

Posted 12 November 2013 - 05:38 AM

System.String[] This is a string array. You can loop through the array using a foreach loop to print them to the console.
Was This Post Helpful? 0
  • +
  • -

#6 vivek20055   User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 82
  • Joined: 02-November 13

Re: Difference between File.ReadAllLines and StreamReader

Posted 12 November 2013 - 06:02 AM

Hi,

Yes, absolutely that works fine. Thank you.

That works with normal text files

But I have log file and excel files for which this File.ReadAllLines is not working.
I am attaching those log files with this message

Can you please tell me why???

Thanks
John

This post has been edited by vivek20055: 12 November 2013 - 06:06 AM

Was This Post Helpful? 0
  • +
  • -

#7 Michael26   User is offline

  • Futurama: Insert funny joke here
  • member icon

Reputation: 414
  • View blog
  • Posts: 1,664
  • Joined: 08-April 09

Re: Difference between File.ReadAllLines and StreamReader

Posted 12 November 2013 - 06:06 AM

You didn't attach anything, as for

Quote

excel files for which this File.ReadAllLines is not working.
you would have to post what have you alerady done and your error message.
Was This Post Helpful? 0
  • +
  • -

#8 vivek20055   User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 82
  • Joined: 02-November 13

Re: Difference between File.ReadAllLines and StreamReader

Posted 12 November 2013 - 06:19 AM

View PostMichael26, on 12 November 2013 - 06:06 AM, said:

You didn't attach anything, as for

Quote

excel files for which this File.ReadAllLines is not working.
you would have to post what have you alerady done and your error message.


Hi,

I do not have any error here.

It is reading the file but it is not showing the correct data in the file.
It is just showing like the image attached with this message in console window.
I am attaching the file with this mail

Thanks John

Hi,

I am also attaching the excel(zipped) data file I am reading.

Can you please tell me why I am getting like this?


Thanks
John

Attached image(s)

  • Attached Image

Attached File(s)

  • Attached File  excel.zip (5.36K)
    Number of downloads: 159

This post has been edited by vivek20055: 12 November 2013 - 06:16 AM

Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7187
  • View blog
  • Posts: 24,357
  • Joined: 05-May 12

Re: Difference between File.ReadAllLines and StreamReader

Posted 12 November 2013 - 06:49 AM

Excel files are not text files. ReadAllLines() is for reading text files.

The .xlsx uses the Office Open XML format: http://en.wikipedia....Office_Open_XML . It's basically a set of XML files put into a zip file.

The older .xls file uses the compound document storage format: http://msdn.microsof...9(v=vs.85).aspx . It's basically a set of data streams put into a file that mimics the FAT file system.
Was This Post Helpful? 2
  • +
  • -

#10 boxed   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 06-November 13

Re: Difference between File.ReadAllLines and StreamReader

Posted 13 November 2013 - 11:49 PM

You should definitely read the links Skydiver posted, but I have a suggestion that may make things easier for you.

You can save the .xls/.xlsx file as a .csv (comma separated values) file and use some of the same methods you're using now. You will have to do some research into string manipulation (and loops by the look of your code, no offense but that 1000000000000 in your for loop is scary looking):

Stings:
http://msdn.microsof...o/ms228362.aspx

Splitting strings:
http://msdn.microsof...o/ms228388.aspx
Was This Post Helpful? 0
  • +
  • -

#11 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: Difference between File.ReadAllLines and StreamReader

Posted 14 November 2013 - 10:28 AM

View PostMichael26, on 12 November 2013 - 04:00 AM, said:

Posted Image

Since ReadAllLines and StreamReader are doing two different things, you can't just compare the times like this. Add the creation and resizing of a String array to the StreamReader code, then you can compare them.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1