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

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …