0

我有一个非常大的文本文件(500mb),我需要获取它的文本。当然问题是内存异常,但我想通过获取字符串(或字符数组)并将它们放入列表中来解决它。我在谷歌搜索,我真的不知道如何参与特定的部分。* 这是一条很长的线,如果有帮助的话。

4

2 回答 2

7

去做:

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())
        }
    }
于 2012-06-28T19:46:17.887 回答
1

您可以使用StreamReader类来读取文件的一部分。

于 2012-06-28T19:42:14.920 回答