C# StreamReader and StreamWriter2 min read

StreamReader and StreamWriterare classes are inherited from the base class Stream, these classes are used to read and write from a file.

You may go through the File I/O.

C# StreamReader Class

The StreamReader class inherits from TExtReader class. It is used for reading data from a file. The most used method in this class are:

  • Close(): It closes the Streamreader object or any resources assosciateed with it.
  • Peek(): It returns the next available character but does not consume it.
  • Read(): It reads the next characer in input stream and increases the charcter position by one.
  • ReadLine(): It reads the line from input stream and returns the data in the form of string.
  • Seek(): This method is used to read or write the data at a specific location from a file.

Example: C# program for StreamReader

Let us create a file called sample.text for this example and the following string is present there:

This is a Website.
This is Simple2code.com. Visit again.

Output: The string present in a sample.text will be displayed on the screen.

This is a Website.
This is Simple2code.com. Visit again.


C# StreamWriter Class

StreamWriter class inherits TextWriter class. it is used to write a series of characters to a stream in a particular format. The most used method in this class are:

  • Close(): This methosd closes the current StreamWriter object and all the resources assosciated with it.
  • Flush(): It clears all the data from the buffer and write it in the stream associate with it.
  • Write(): It is used to write data to a stream.
  • WriteLine(): It writes the the data to a stream and adds the newline character at the end of the data.

Example: C# program for StreamWriter

The following program creates a new file and writes data into that file. The data is provided in the program.

Output:

The file is created.

After execution, open the newly created file “sample.text”, you will find the following data.

This is Simple2code.com. Visit again.


MORE

Keith Number Program in C

In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you …
Read More

Keith Number Program in C++

In this tutorial, we will write a Keith number in C++. It is one of the most asked questions in an interview. Before that, you …
Read More

Singly linked list Program C++

In this tutorial, we will write a C++ program to implement singly linked list. In this program, we will create a separate function for each …
Read More

Fascinating Number Program in C

In this tutorial, we will write a C program to check whether the given number is a Fascinating number or not. You may go through …
Read More

Peterson Number in C

In this tutorial, we will learn about the Peterson number and check if the number is Peterson or not in C. What is Peterson Number? …
Read More

Peterson Number in C++

In this tutorial, we will learn about the Peterson number and write a C++ program to check if the given number is a Peterson number …
Read More