text file: Reading line by line C#

2019-02-12 16:45发布

So, let's say i have a text file with 20 lines, with on each line different text. i want to be able to have a string that has the first line in it, but when i do NextLine(); i want it to be the next line. I tried this but it doesn't seem to work:

string CurrentLine; 
int LastLineNumber;   
Void NextLine() 
{
     System.IO.StreamReader file = new System.IO.StreamReader("c:\\test.txt");
     CurrentLine = file.ReadLine(LastLineNumber + 1);
     LastLineNumber++;
}

How would i be able to do this? Thanks in advance.

5条回答
Summer. ? 凉城
2楼-- · 2019-02-12 16:59

One simple call should do it:

var fileLines = System.IO.File.ReadAllLines(fileName);

You will want to validate the file exists and of course you still need to watch for blank lines or invalid values but that should give you the basics. To loop over the file you can use the following:

foreach (var singleLine in fileLines) {
   // process "singleLine" here
}

One more note - you won't want to do this with large files since it processes everything in memory.

查看更多
Melony?
3楼-- · 2019-02-12 16:59

The ReadLine method already reads the next line in the StreamReader, you don't need the counter, or your custom function for that matter. Just keep reading until you reach your 20 lines or until the file ends.

查看更多
做自己的国王
4楼-- · 2019-02-12 17:08

You can't pass a line number to ReadLine and expect it to find that particular line. If you look at the ReadLine documentation, you'll see it doesn't accept any parameters.

public override string ReadLine()

When working with files, you must treat them as streams of data. Every time you open the file, you start at the very first byte/character of the file.

var reader = new StreamReader("c:\\test.txt");      // Starts at byte/character 0

You have to keep the stream open if you want to read more lines.

using (var reader = new StreamReader("c:\\test.txt"))
{
    string line1 = reader.ReadLine();
    string line2 = reader.ReadLine();
    string line3 = reader.ReadLine();
    // etc..
}

If you really want to write a method NextLine, then you need to store the created StreamReader object somewhere and use that every time. Somewhat like this:

public class MyClass : IDisposable
{
    StreamReader reader;

    public MyClass(string path)
    {
        this.reader = new StreamReader(path);
    }

    public string NextLine()
    {
        return this.reader.ReadLine();
    }

    public void Dispose()
    {
        reader.Dispose();
    }
}

But I suggest you either loop through the stream:

using (var reader = new StreamReader("c:\\test.txt"))
{
    while (some_condition)
    {
        string line = reader.ReadLine();
        // Do something
    }
}

Or get all the lines at once using the File class ReadAllLines method:

string[] lines = System.IO.File.ReadAllLines("c:\\test.txt");
for (int i = 0; i < lines.Length; i++)
{
    string line = lines[i];
    // Do something
}
查看更多
别忘想泡老子
5楼-- · 2019-02-12 17:10

Well, if you really don't mind re-opening the file each time, you can use:

CurrentLine = File.ReadLines("c:\\test.txt").Skip(LastLineNumber).First();
LastLineNumber++;

However, I'd advise you to just read the whole thing in one go using File.ReadAllLines, or perhaps File.ReadLines(...).ToList().

查看更多
爷的心禁止访问
6楼-- · 2019-02-12 17:18

In general, it would be better if you could design this in a way to leave your file open, and not try to reopen the file each time.

If that is not practical, you'll need to call ReadLine multiple times:

string CurrentLine; 
int LastLineNumber;   
void NextLine() 
{
    // using will make sure the file is closed
    using(System.IO.StreamReader file = new System.IO.StreamReader("c:\\test.txt"))
    {
        // Skip lines
        for (int i=0;i<LastLineNumber;++i)
            file.ReadLine();

        // Store your line
        CurrentLine = file.ReadLine();
        LastLineNumber++;
    }
}

Note that this can be simplified via File.ReadLines:

void NextLine() 
{
    var lines = File.ReadLines("C:\\test.txt");

    CurrentLine = lines.Skip(LastLineNumber).First();
    LastLineNumber++;
}
查看更多
登录 后发表回答