File handling in c#


What is file in c# ?

A file is a collection of data consisting of a specific or unique name with a path containing a directory name that helps access it. After opening the file, it becomes a stream, and the stream is a sequence of bytes in the communication path.

What is file handling in c# ?

like creating a file, deleting a file, updating a file, appending a file etc. The file becomes a stream when we open it for reading or writing, and the stream is a sequence of bytes that we use for communication.

There are various classes and methods of those classes which we can use to perform operations on files.

StreamWriter class

StreamWriter is a class which implements TextWriter to write in a file. The methods are as follows,

  • Close() : Closes the current stream writer object.
  • Flush() : Clears all the data from the buffer.
  • Write() : Write in the file.
  • WriteLine() : It also works the same as write() but it adds a newline character at the end of line.

StreamReader :

This class implements TextReader in file and reads the file. There are some methods in this class, 

  • Close() : It closes the current StreamReader object.
  • Peek() : It returns the next available character but does not consume it.
  • Read() : Read the next character and increment by one in the stream. 
  • ReadLine() : Reads a line from input and  returns the data in the form of string.
  • Seek() : It is used to read or write at any specific location.