How to read a specific part in text file?

后端 未结 2 554
傲寒
傲寒 2020-12-22 11:46

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 a

相关标签:
2条回答
  • 2020-12-22 12:03

    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())
            }
        }
    
    0 讨论(0)
  • 2020-12-22 12:13

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

    0 讨论(0)
提交回复
热议问题