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

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a numberĀ using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

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 …