Frage

I have a text file which i am reading using StreamReader .Now as per my requirement whatever lines i have read first,i dont want to read again means i dont want to take that data again.So i have added File.ReadLines(FileToCopy).Count(); code to get the number of lines read at first.Now whatever line returned by above line of code,i want to read after that. Here is my code .

        string FileToCopy = "E:\\vikas\\call.txt";

        if (System.IO.File.Exists(FileToCopy) == true)
        {

            lineCount = File.ReadLines(FileToCopy).Count();

            using (StreamReader reader = new StreamReader(FileToCopy))
            {

            }
         }

What Condition i need to specify here .Please help me.

       while ((line = reader.ReadLine()) != null)
        {

            var nextLines = File.ReadLines(FileToCopy).Skip(lineCount);

        if (line != "")
        {
        }
War es hilfreich?

Lösung

There's a much faster way to do this that doesn't require you to read the entire file in order to get to the point where you left off. The key is to keep track of the file's length. Then you open the file as a FileStream, position to the previous length (i.e. the end of where you read before), and then create a StreamReader. So it looks like this:

long previousLength = 0;

Then, when you want to copy new stuff:

using (var fs = File.OpenRead(FileToCopy))
{
    // position to just beyond where you read before
    fs.Position = previousLength;

    // and update the length for next time
    previousLength = fs.Length;

    // now open a StreamReader and read
    using (var sr = new StreamReader(fs))
    {
        while (!sr.EndOfStream)
        {
            var line = sr.ReadLine();
            // do something with the line
        }
    }
}

This will save you huge amounts of time if the file gets large. For example if the file was a gigabyte in size the last time you read it, then File.ReadLines(filename).Skip(count) will take you 20 seconds to get to the end so you can read the next lines. The method I described above will take much less time--probably less than a second.

Andere Tipps

This:

lineCount = File.ReadLines(FileToCopy).Count();

Will return total lines count in your file.It's useless for you.You need to store the line count that you read from the file.Then everytime you read again, use Skip method:

var nextLines = File.ReadLines("filaPath").Skip(lineCount);

You don't need StreamReader here.For example if you read file for first time,let's say 10 line:

var lines = File.ReadLines(filePath).Take(10);
lineCount += 10;

For second time Skip the first 10 line and read more and update the lineCount:

var nextLines = File.ReadLines(filePath).Skip(lineCount).Take(20);

lineCount += 20;

More generically you can write a method for this and call it whenever you want to read next lines:

public  static string[] ReadFromFile(string filePath, int count, ref int lineCount)
{
    lineCount += count;
    return File.ReadLines(filePath).Skip(lineCount).Take(count).ToArray();
}

private static int lineCount = 0;
private static void Main(string[] args)
{
   // read first ten line
   string[] lines = ReadFromFile("sample.txt", 10, ref lineCount);

   // read next 30 lines
   string[] otherLines = ReadFromFile("sample.txt", 30, ref lineCount)
}

I hope you get the idea.

Just read lineCount lines from your new stream:

for(int n=0; n<lineCount; n++) 
{
    reader.ReadLine();
}

That is the easiest method, when you have to actually skip N lines (not N bytes).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top