StreamReader. This C# class reads text files. It is found in the System.IO namespace. StreamReader is a fast and efficient way to handle text files in this language.
StreamReader is often used with the using-statement, a language construct that helps dispose of system resources. We often place a loop inside "using."
First example. We use the StreamReader type inside a using-statement. This statement allows you to leave the file disposal and opening routines to the C# compiler's knowledge of scope.
Start The "using" statement will be compiled to instructions that tell the runtime to do all the cleanup works on the Windows file handles.
Info A scope is a block between brackets. When objects go out of scope, the garbage collector or finalization code is run.
Thus Scope allows the compiler to make many assumptions about your program. It can tell where the object becomes unreachable.
using System;
using System.IO;
// ... The StreamReader will free resources on its own.
string line;
using (StreamReader reader = new StreamReader("file.txt"))
{
line = reader.ReadLine();
}
Console.WriteLine(line);First line of your file.txt file.
While loop. Next, we see an alternate syntax form for the inner-loop in the StreamReader. It uses an if-statement with a break in it.
using System;
using System.IO;
using (StreamReader reader = new StreamReader("C:\\programs\\file.txt"))
{
while (true)
{
string line = reader.ReadLine();
if (line == null)
{
break;
}
Console.WriteLine(line); // Use line.
}
}123
Bird
Hello friend
List. It is possible to put an entire file into a collection. One requirement is that you need to read in a file line-by-line. You can store all those lines in a generic List or ArrayList.
using System;
using System.Collections.Generic;
using System.IO;
//// Read in a file line-by-line, and store it all in a List.//
List<string> list = new List<string>();
using (StreamReader reader = new StreamReader("file.txt"))
{
string line;
while ((line = reader.ReadLine()) != null)
{
list.Add(line); // Add to list.
Console.WriteLine(line); // Write to console.
}
}First line of your file.txt file.
Second line.
Third line.
Last line.
Close. This is an error-prone way of using StreamReader. We open a file in one line, deal with the file, and then make a call to close it. The code here is cumbersome and unclear.
Warning There are problems with this style of code. It is harder to type and read, and bugs could creep into the code.
However Other than those problems, it works well. If the file is not there, it will throw an exception.
Important If the exception is thrown, you must have a finally block to release the unmanaged resources.
using System;
using System.IO;
// ... Read a line from a file the old way.
StreamReader reader = new StreamReader("file.txt");
string line = reader.ReadLine();
reader.Close();
// ... We should call Dispose on "reader" here, too.
reader.Dispose();
Console.WriteLine(line);First line of file.txt file.
Summary. StreamReader easily reads in lines of text files. The using-keyword has good performance. The base class library's file IO methods are efficient. They are well-designed.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Nov 10, 2023 (simplify).