liran63
liran63

Reputation: 1336

How to read a specific part in text file?

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.

Upvotes: 0

Views: 5796

Answers (2)

eyossi
eyossi

Reputation: 4330

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())
        }
    }

Upvotes: 7

Volodymyr Dombrovskyi
Volodymyr Dombrovskyi

Reputation: 269

You can use StreamReader class to read parts of a file.

Upvotes: 1

Related Questions