Generally, the file is used to store the data. The term File Handling refers to the various operations like creating the file, reading from the file, writing to the file, appending the file, etc. There are two basic operation which is mostly used in file handling is reading and writing of the file. The file becomes stream when we open the file for writing and reading. A stream is a sequence of bytes which is used for communication. Two stream can be formed from file one is input stream which is used to read the file and another is output stream is used to write in the file. In C#, System.IO namespace contains classes which handle input and output streams and provide information about file and directory structure.
Here we are going to discuss about two classes which are useful for writing in and reading from the text file.
The StreamWriter class implements TextWriter for writing character to stream in a particular format. The class contains the following method which are mostly used.
|Close()||Closes the current StreamWriter object and stream associate with it.|
|Flush()||Clears all the data from the buffer and write it in the stream associate with it.|
|Write()||Write data to the stream. It has different overloads for different data types to write in stream.|
|WriteLine()||It is same as Write() but it adds the newline character at the end of the data.|
Output: You will find the file at the specified location having the content:
The StreamReader class implements TextReader for reading character from the stream in a particular format. The class contains the following method which are mostly used.
|Close()||Closes the current StreamReader object and stream associate with it.|
|Peek()||Returns the next available character but does not consume it.|
|Read()||Reads the next character in input stream and increment characters position by one in the stream|
|ReadLine()||Reads a line from the input stream and return the data in form of string|
|Seek()||It is use to read/write at the specific location from a file|
- Basics Operations of File and Directory in C#
- C | File Handling | Question 1
- C | File Handling | Question 2
- C | File Handling | Question 3
- C | File Handling | Question 4
- C | File Handling | Question 5
- Basics of FileStream in C#
- Exception Handling in C#
- How to write your own header file in C?
- File.GetLastWriteTimeUtc() Method in C# with Examples
- File opening modes(r versus r+)
- C++ program to create a file
- File.Create(String, Int32, FileOptions, FileSecurity) Method in C# with Examples
- Check if a path has a file name extension in C#
- File.Open(String, FileMode, FileAccess) Method in C# with Examples
- File.Open(String, FileMode, FileAccess, FileShare) Method in C# with Examples
- How to Read and Write a Text File in C#?
- File.Copy(String, String) Method in C# with Examples
- File.Move() Method in C# with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.