I have a really big text file (500mb) and i need to get its text. Of course the problem is the exception-out of memory, but i want to solve it with taking strings (or char arrays) and put them in List. I search in google and i really don't know how to take a specific part. * It's a one long line, if that helps.
Asked
Active
Viewed 5,571 times
0
-
What's the format of this file? Which part are you interested in? How is it delimited? – Darin Dimitrov Jun 28 '12 at 19:39
-
It sounds like you will have to read the file using a byte buffer and test along the way whether you have reached the part of interest. It would help to be more specific about your requirements. – harpo Jun 28 '12 at 19:40
-
If you want the entire text, you could instead go line by line. Otherwise, if you're looking for a specific portion of text, you might try an indexof. – Ari Jun 28 '12 at 19:41
-
Try reading this question: http://stackoverflow.com/questions/3816789/read-from-streamreader-in-batches-c It is quite similar to what you're asking. – Ari Jun 28 '12 at 19:44
-
In the title you say specific part in text file, but in the detail, you just need its text, seeming to say you need it all, can you elaborate. If you need a specific amount, how will you determine it? Key words or phrases, line number? You will need StreamReader, StringBuilder, your List, and maybe a Regex. – GrayFox374 Jun 28 '12 at 19:57
2 Answers
7
Do that:
using (FileStream fsSource = new FileStream(pathSource,
FileMode.Open, FileAccess.Read))
{
// Read the source file into a byte array.
int numBytesToRead = // Your amount to read at a time
byte[] bytes = new byte[numBytesToRead];
int numBytesRead = 0;
while (numBytesToRead > 0)
{
// Read may return anything from 0 to numBytesToRead.
int n = fsSource.Read(bytes, numBytesRead, numBytesToRead);
// Break when the end of the file is reached.
if (n == 0)
break;
// Do here what you want to do with the bytes read (convert to string using Encoding.YourEncoding.GetString())
}
}

eyossi
- 4,230
- 22
- 20
1
You can use StreamReader class to read parts of a file.

Volodymyr Dombrovskyi
- 269
- 2
- 9
-
Just read up to a line ending (`\n` or `\r`) then parse the string. – Cole Tobin Jun 28 '12 at 19:44
-
@ColeJohnson, but the OP said that there's only 1 line in the file. – Darin Dimitrov Jun 28 '12 at 19:45
-
@DarinDimitrov to me, it meant that there is one line in a collection of many. like a dictionary. – Cole Tobin Jun 28 '12 at 19:47
-
@ColeJohnson, I don't know. To me `It's a one long line, if that helps.` means that the file contains only one long line of 500MB. But I might be wrong of course. – Darin Dimitrov Jun 28 '12 at 19:55