C# StreamReader

WBOY
WBOYOriginal
2024-09-03 15:23:26435browse

To read the characters into a stream that follows a specific encoding, we make use of the class called StreamReader class in C# and the method StreamWriter.Read() method of StreamReader class is responsible for reading the next character or the next set of characters from the stream. TextReader class is the base class of StreamReader class that is the StreamReader class is inherited from the TextReader class and this TextReader class provides several methods which can be used to read a character, block, line, etc. and System.IO.namespace is the namespace in which the StreamReader is defined and StreamReader class provides several read methods such as Peak, Read, ReadAsync, ReadBlock, ReadBlockAsync, ReadLine, ReadLineAsync, ReadToEnd, ReadToEndAsync, etc.

Syntax:

The syntax of the StreamReader class in C# is as follows:

public class StreamReader: System.IO.TextReader

Functions of StreamReader Class in C#

  • Data is read from the files using Streams in C#.
  • The stream is the extra layer between the application and the file.
  • Data from the file can be read smoothly by making use of the stream.
  • The stream receives the small chunks of data that is broken down from the larger files. The application can read these small chunks of data from the streams and it does not have to read all the data directly from the larger file
  • There is an impact on the performance of the application if the application tries to read all the data from the larger files.
  • Hence, the data must be read through the stream rather than the large files itself.

Consider the example below to explain the usage of StreamReader to read data from a file:

Examples of C# StreamReader

Following are the examples of c# streamreader

Example #1

Code:

using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//a namespace called program is defined
namespace program
{
//a class called check is defined
class check
{
//main method is called
static void Main(string[] args)
{
//the path of the file and the file name is assigned to a string variable
String def = @"D:\imp.txt";
//an instance of the string writer class is created, and the path of the file is passed as a parameter to append text to the file
using (StreamWriter stwr = File.AppendText(def))
{
//data to be appended to the file is included
stwr.WriteLine("Welcome to StreamWriter class in C#");
//the instance of the streamwriter class is closed after writing data to the File
stwr.Close();
try
{
// an instance of stream reader class is created, and data is read from the file by taking the path of the file as parameter
using (StreamReader read = new StreamReader(def))
{
//a string variable is defined
string line1;
// Data is being read one line after the other
while ((line1 = read.ReadLine()) != null)
{
Console.WriteLine(line1);
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.ReadKey();
}
}
}
}

Output:

C# StreamReader

In the above program, the program is the namespace defined. Then check is the class defined. Then the main method is called. Then the string variable assigned the filename and the file path. Then string writer class instance is created, and the file path is passed as a parameter to write text to the file. Then data to be written to the file is included. Then the stream writer class instance is closed after writing data to the File. Then an instance of stream reader class is created, and data is read from the file by taking the path of the file as a parameter. Then a string variable is defined. Then the data is being read one line after the other. The output of the program is as shown in the snapshot above.

Example #2

C# program to explain the usage of StreamReader class:

Code:

using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//a class called check is defined
class check
{
//main method is called
static void Main()
{
String fin = @"D:\Ex.txt"
//an instance of streamwriter class is created and the path of the file is passed as a parameter
using (StreamWriter stwf = new StreamWriter(fin))
{
//write() method of stream writer class is used to write the first line so that the next line continues from here
stwf.Write("Welcome to StreamWriter class in C# and ");
//writeline() method is used to write the second line and the next line starts from a new line
stwf.WriteLine("this program is demonstration of StreamWriter class in C# ");
//writeline() method is used to write the third line and the next line starts from a new line
stwf.WriteLine("I hope you are learning ");
stwf.Dispose();
try
{
// an instance of stream reader class is created, and data is read from the file by taking the path of the file as parameter
using (StreamReader read = new StreamReader(fin))
{
//a string variable is defined
string line1;
// Data is being read one line after the other
while ((line1 = read.ReadLine()) != null)
{
Console.WriteLine(line1);
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.ReadKey();
}
}
}

Output:

C# StreamReader

In the above program, the program is the namespace defined. Then check is the class defined. Then the main method is called. Then the string variable assigned the filename and the file path. Then string writer class instance is created, and the file path is passed as a parameter to write text to the file. Then the write() method of stream writer class is used to write the first line so that the next line continues from here. Then the writeline() method is used to write the second line and the next line starts from a new line. Then the writeline() method is used to write the third line and the next line starts from a new line. Then an instance of stream reader class is created, and data is read from the file by taking the path of the file as a parameter. Then a string variable is defined. Then the Data is being read one line after the other until the end of the line. The output of the program is as shown in the snapshot above.

The above is the detailed content of C# StreamReader. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:C# StringWriterNext article:C# StringWriter