C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
It returns the next text line from the file. By processing each line separately, we can apply logic to test the lines for validity or reject them. This avoids unnecessary complexity.
Example. First we look at the ReadLine method on StreamReader and how you can use it with List. The .NET Framework doesn't provide a "Read All Lines" method on StreamReader, but our solution will fill that need.
Note: If you need a complete array of lines from a file, you can use the static File.ReadAllLines method.
C# program that uses ReadLine and List using System; using System.Collections.Generic; using System.IO; class Program { static void Main() { const string f = "TextFile1.txt"; // 1 // Declare new List. List<string> lines = new List<string>(); // 2 // Use using StreamReader for disposing. using (StreamReader r = new StreamReader(f)) { // 3 // Use while != null pattern for loop string line; while ((line = r.ReadLine()) != null) { // 4 // Insert logic here. // ... // "line" is a line in the file. Add it to our List. lines.Add(line); } } // 5 // Print out all the lines. foreach (string s in lines) { Console.WriteLine(s); } } } Output (Prints contents of TextFile1.txt) This is a text file I created, Just for this article.
In this example, we use List, which is a generic object that expands to fit your data. The List data structure will easily accommodate files from 0 to 1,000,000+ lines long. It is perfect in this program context.
Tip: For StreamReader, we use the using keyword to read the lines. This provides automatic disposal, releasing important resources.
Next, the syntax for ReadLine is complicated. We first assign the string to the ReadLine result string. That string will be null if we are done reading the file. We need a Boolean result for an if-statement, so we test against null.
List: We call Add on the instance of List here. This appends another string to the List.
Finally: We display the results to the console with Console.WriteLine. You will see all the lines in the file displayed here.
Note: This can catch some really simple bugs, such as those caused by an invalid filename.
IOException. You will want to catch IOException if your application's stability is critical. You could catch all IO exceptions and return an empty array if the file cannot be read. You may have more detailed requirements, but IOException works well.
Discussion. This code gives you a chance to test each line, as with a regular expression, before adding it to the array. You could use File.ReadAllLines to consume the file into an array, but you can't test each line first that way.
Using arrays instead. When you are done with the testing of the file line-by-line with ReadLine, and have added all the correct lines to the List, you can convert the List to an array, as with ToArray().
Summary. We saw here a common approach of reading each line into a file line-by-line. This can provide superior memory management over allocating an entire array. It offers an opportunity for you to test each line before storing it.