[英]How to read a specific part in text file?
I have a really big text file (500mb) and i need to get its text. 我有一个非常大的文本文件(500mb),我需要获取其文本。 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. 当然问题是内存不足,但是我想通过采用字符串(或char数组)并将其放入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. *如果需要的话,这是一条长长的线。
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())
}
}
您可以使用StreamReader类读取文件的各个部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.