0

我需要读取从 1mb 到 2gb 的各种大小的文件,因为这些文件的大小可能很大,我正在流式传输它。

  • 我如何知道读取了多少文件并跟踪它而无需预先读取所有行?

示例代码

int count = 0;
using (Stream stream = File.OpenRead(filename))
{
    using (StreamReader reader = new StreamReader(stream))
    {
        string item = string.Empty;
        while ((item = reader.ReadLine()) != null)
        {
            item = item.Replace("\"", ""); // remove unwanted double quotes
            if (item.Length < 2) // dont need lines with less then 2 char
                continue;

            if (fine add to db) 
                count++; // to keep track of good lines
        }
    }
}
4

2 回答 2

0

您应该使用类"Position"中覆盖"FileStream"的属性"Stream"

看看这里

于 2012-11-11T21:18:50.723 回答
0
var remaining = yourFile.Length;
var goodLines = File.ReadLines(yourFile.FullName)
                    .Select(line => line.Replace("\"", ""))
                    .Where(line => line.Length > 1);

foreach(var goodLine in goodLines)
{
   // do something with goodLine

   // this will be slightly off since you remove quotes.
   remaining -= (goodLine.Length + Environment.NewLine.Length);
}

MSDN 文件.ReadLines

于 2012-11-11T21:45:47.560 回答