File Handling in C# with Examples
In this article, I am going to discuss File Handling in C# with Examples. At the end of this article, you will understand what is File Handling and why we need File Handling, and how to implement File Handling in C# using StreamWriter and StreamReader class with Examples.
What is a File?
A file is a collection of data stored on a disk with a specific name, extension, and directory path. When you open File using C# for reading and writing purposes it becomes Stream.
What is Stream?
A stream is a sequence of bytes traveling from a source to a destination over a communication path. There are two main streams: the input stream and the output stream. The input stream is used for reading data from the file (read operation) and the output stream is used for writing into the file (write operation). There are two types of streams are used:
- Input stream: This stream is used to read data from a file, which is known as a read operation.
- Output stream: This stream is used to write data into a file, which is known as a write operation.
Why do I need to Learn File Handling in C#?
As a C# programmer, several times you need to save information on a disk. You will not get a database everywhere to save the information and your project may require saving information in a text file, doc file, xls file, pdf file, or any other file types. So, you must know the concept of saving data in a file.
File Handling in C#
Generally, we use the file to store data. The term File Handling in C# refers to the various operations that we can perform on a file such as creating a file, reading data from the file, writing data into the file, appending the file, etc.
Generally, two basic operations we mostly perform on a file that is reading data from a file and writing data to a file. One more thing that you need to remember the file becomes a stream when we open the file either for writing or for reading.
Then the next question that should come to your mind is what is a stream? In simple words, we can say that a stream is a sequence of bytes that is used for communication. When you open a file for reading or writing, it becomes a stream. A stream is a sequence of bytes traveling from a source to a destination over a communication path.
There are two types of streams for a single file. One is the Input stream which is used for reading the file and the other one is the Output stream which is used for writing the file.
System.IO Namespace in C#
In C#, The System.IO namespace contains the required classes which are used to handle the input and output streams and also provide information about file and directory structure. The parent class of file processing is Stream. Stream is an abstract class, which is used as the parent of the classes that actually implement the necessary operations.
Please have a look at the below image which shows the file handling class hierarchy in C#.
Note: The FileIno, DirectoryInfo, and DriveInfo classes have instance methods. File, Directory, and Path classes have static methods. The following table describes commonly used classes in the System.IO namespace.
In the next article, I am going to discuss How to Implement File Handling using FileSteam Class in C# with Examples. Here, in this article, I try to explain File Handling in C# with examples. I hope you enjoy this File Handling in C# using StreamWriter and StreamReader class with Examples article. I would like to have your feedback. Please post your feedback, question, or comments about this article.