Using StreamReader.ReadToEnd() to read flat file.

stoymigo

Senior Member
Joined
Dec 11, 2008
Messages
975
Reaction score
26
Hi, i use the following code to read a flat file faster than reading line by line, and I store its contents
into a RichTextBox that won't be seen.

Code:
      RichTextBox rch = new RichTextBox();
      StreamReader sr;
      sr = new StreamReader(inputFilePath);
      rch.Text = sr.ReadToEnd();

The reason I use richtextbox is because I can iterate through its line items once its populated, do you know of a collection I can use instead of richtextbox because it's a form control and I'm not using this code in a form, plus I'm not sure it's a good practice.

Do you know of any collection i can use bcos I can't seem to dump a file into a string array or arraylist, I'd have to loop through the file which is slow.
Thanks.
 
I have no idea what language this is, but I will say with certainty that ReadToEnd just encapsulates the loop you want to avoid. This is the nature of a stream, you have to traverse/consume it to get the complete "picture".

If you can, look at the source of ReadToEnd. Also look at eg richtext's LoadFromFile or equivalent method. Loops are "slow" on 286's. dual/quad core machines running 2ghz plus, no so much :)
 
Last edited:
How about something like:

String[] x = streamreader.readtoend().split(enviroment.newline);

On my phone right now, so excuse the case.
 
Last edited:
Hi, i use the following code to read a flat file faster than reading line by line, and I store its contents
into a RichTextBox that won't be seen.

Code:
      RichTextBox rch = new RichTextBox();
      StreamReader sr;
      sr = new StreamReader(inputFilePath);
      rch.Text = sr.ReadToEnd();

The reason I use richtextbox is because I can iterate through its line items once its populated, do you know of a collection I can use instead of richtextbox because it's a form control and I'm not using this code in a form, plus I'm not sure it's a good practice.

Do you know of any collection i can use bcos I can't seem to dump a file into a string array or arraylist, I'd have to loop through the file which is slow.
Thanks.

Try something like this:

Code:
List<string> fileLines = new List<string>();
using (StreamReader reader = new StreamReader(inputFilePath))
{
    string content = reader.ReadToEnd();
    fileLines = content.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None).ToList();      
}



Include the following in the using directives:
Code:
using System.Collections.Generic;
 
Last edited:
Why not use one of the automatic functions?

string content = IO.File.ReadAllText(Filename);
or
string lines[] = IO.File.ReadAllLines(Filename);
 
I have no idea what language this is, but I will say with certainty that ReadToEnd just encapsulates the loop you want to avoid. This is the nature of a stream, you have to traverse/consume it to get the complete "picture".

If you can, look at the source of ReadToEnd. Also look at eg richtext's LoadFromFile or equivalent method. Loops are "slow" on 286's. dual/quad core machines running 2ghz plus, no so much :)

it are Java

edit: it are not java :(
 
Last edited:
it are Java

Doubtful, if it were Java then the method names would be lower case eg:
readToEnd();

Also Java streams can't and won't offer a method readToEnd() simply because a stream cannot guarantee that sort of functionality. Only .NET would add a read to end to a stream :p
 
Doubtful, if it were Java then the method names would be lower case eg:
readToEnd();

Also Java streams can't and won't offer a method readToEnd() simply because a stream cannot guarantee that sort of functionality. Only .NET would add a read to end to a stream :p

ok good points :p hey I am a noobie to programming and have started by learning Java so to my nooish eye it looks very very similar.
 
Top
Sign up to the MyBroadband newsletter
X