简体   繁体   中英

Read only first line from a text file

so what I'm failing to do is, MyFile.txt has either "english", "french" or "german" in the first line and I want to get the language from the first line of the text file, then continue my code

String[] languages = new String[] { "english", "french", "german"};

foreach (String language in languages)
{
    string line1 = File.ReadLines("MyFile.txt").Skip(0).Take(1);
    line1 = language;
    continue;
}

You can make use of File.ReadLines together with Enumerable.First . This guarantees you to only read the first line from the file.

using System.Linq; 

...

string line1 = File.ReadLines("MyFile.txt").First(); // gets the first line from file.

The difference to File.ReadAllLines is, that File.ReadLines makes use of lazy evaluation and doesn't read the whole file into an array of lines first.

Linq also makes sure of properly disposing the FileStream.

To comment on the use of ReadAllLines() in the OP's comment on the answer of CSharpie ; it may have a huge impact on the performance if MyFile.txt is a very large file.

File.ReadAllLines().First() will actually read all the lines, store them in a string[] and then take the first. Therefore, if your file is very large, it will store all these lines in the array, which might take some time.

An alternative and better performing option would be to just open a StreamReader and read only the first line. A correct implementation would be;

String[] languages = new String[] { "english", "french", "german"};
string firstLine;

using(StreamReader reader = new StreamReader("MyFile.txt"))
{
    firstLine = reader.ReadLine() ?? "";
}

if(languages.Contains(firstLine))
{
    //...
}

The use of using will take care of closing and disposing the reader. Also, using ?? will make sure null is never returned (and thus saving you an ArgumentNullException on Contains() ).

Though the post is from 2014, a more efficient solution using a more recent method could be this one:

System.IO.StreamReader readingFile = new System.IO.StreamReader(filePath);

string readingLine = readingFile.ReadLine();

This way you prevent reading several lines and needing to get the first one with Linq.

If the file is already opened by another process and concurrent reading is allowed, then something like this is needed. Otherwise a share violation exception is thrown.

// Usage of "FileAccess.Read, FileShare.ReadWrite" prevents an avoidable
// exception when file is already opened with concurrent reading by 
// another process
using (var fileStream = new FileStream(@"MyFile.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
    using (var readerForFileStream = new StreamReader(fileStream))
    {
        string firstLine = readerForFileStream.ReadLine();

        if (firstLine != null)
        {
            // the file has a line
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM