Files are used to store the data permanently in the storage device with a specific name and path. And when we open the file in order to read or write, it becomes a stream.
File Handling refers to the various operation that can be performed on a file such as reading from a file, writing to a file, appending file, etc. The two most common operations are reading from a file and writing to a file. And when we open a file to do so then the file becomes a stream.
System.IO namespace class contains the input and output streams handling classes that are used for performing the various operation.
The members that are included in
System.IO namespace classes are:
|Reads primitive data from a binary stream.|
|Writes primitive data in binary format.|
|Temporary storage for a stream of bytes.|
|Helps in manipulating a directory structure.|
|DirectoryInfo||Used for performing operations on directories.|
|Provides information for the drives.|
|Helps in manipulating files.|
|Used for performing operations on files.|
|Used to read from and write to any location in a file.|
|Used for random access to streamed data stored in memory.|
|Performs operations on path information.|
|Used for reading characters from a byte stream.|
|Is used for writing characters to a stream.|
|Is used for reading from a string buffer.|
|Is used for writing into a string buffer.|
As already discussed above, FileStream class provides a stream for the various file operations. Reading and writing data into a file becomes easy with FileStream.
However, a FileStream object is required in order to create a new file or open an existing one. To create a FileStream object we use a
new keyword in the following way.
FileStream <object_name> = new FileStream( <file_name>, <FileMode Enumerator>,
<FileAccess Enumerator>, <FileShare Enumerator>);
FileStream file = new FileStream("example.txt", FileMode.Open, FileAccess.Read,
Let us see what each of the above Enumerator means:
This emulator defines the various ways through which we can open a file and its member are:
|Append||It creates a new file or opens a file(if exists any) and puts the cursor at the end of the file.|
|Create||It is used to create a new file.|
|CreateNew||This member specifies the OS that it should create a new file.|
|Open||It is used to open an existing file.|
|OpenOrCreate||It is used to open an existing file, if not then create a new one.|
|Truncate||It opens an existing file and truncates its size to zero bytes.|
This enumerator has three members:
- Read: Only to read.
- ReadWrite: Both read and write
- Write: Only write.
FileShare refers to sharing of files and its members are:
|Inheritable||It allows a filehandle to pass an inheritance to the child processes|
|None||Sharing of the current file is declined by this member.|
|Read||It opens a file only for reading.|
|ReadWrite||It opens a file for both reading and writing|
|Write||It opens a file only for writing.|
Let us g through an example to see it in action.
Example: C# program for FileStream
The following program writes multiple bytes into the file
static void Main(string args)
FileStream f = new FileStream("sample.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
for (int i = 97; i <= 122; i++)
Output: Once the program is executed, a new file will be created if it doesn’t already exist. And inside that file, the following text will be present.
String Pattern Programs in C
Java Program to Find pair of Integers in Array whose sum is given Number
Program to Print Diamond Alphabet Patterns in C
Half Diamond Pattern in C using Alphabets
Half Pyramid of Alphabets in C