|
-
Jul 21st, 2012, 02:32 PM
#12
Re: Creating lines of a text file into an array?
 Originally Posted by rjv_rnjn
In most cases ReadAllLines() will suffice. But this can come back if your file sizes are too big. Say if you are reading a file of 100MB, you would not want to read the entire file in memory at once. In that case you may want to read that line by line and store each line in a list.
You can store your lines as a List of strings. Something like,
Code:
List<string> fileLines = new List<string>();
while ((line = oStreamReader.ReadLine()) != null)
{
fileLines.Add(line);
}
I know this may be a bit too much to begin with but keep in mind to check Generics later.
There are a whole list of collection classes in .Net that you can use where you don't have to specify the length of the collection in advance.
http://msdn.microsoft.com/en-us/library/6tc79sx1.aspx
I would use a List<T> capacity if that were the case, this would still load ~100MB into memory, which may not be what you want, load items up to the capacity, then deal with them and continue loading up to the capacity, then repeat.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|