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.
The 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:
I/OClass | Description |
---|---|
BinaryReader | Reads primitive data from a binary stream. |
BinaryWriter | Writes primitive data in binary format. |
BufferedStream | Temporary storage for a stream of bytes. |
Directory | Helps in manipulating a directory structure. |
DirectoryInfo | Used for performing operations on directories. |
DriveInfo | Provides information for the drives. |
File | Helps in manipulating files. |
FileInfo | Used for performing operations on files. |
FileStream | Used to read from and write to any location in a file. |
MemoryStream | Used for random access to streamed data stored in memory. |
Path | Performs operations on path information. |
StreamReader | Used for reading characters from a byte stream. |
StreamWriter | Is used for writing characters to a stream. |
StringReader | Is used for reading from a string buffer. |
StringWriter | Is used for writing into a string buffer. |
C# FileStream
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.
1 2 3 4 5 6 | FileStream <object_name> = new FileStream( <file_name>, <FileMode Enumerator>, <FileAccess Enumerator>, <FileShare Enumerator>); //Example FileStream file = new FileStream("example.txt", FileMode.Open, FileAccess.Read, FileShare.Read); |
Let us see what each of the above Enumerator means:
FileMode:
This emulator defines the various ways through which we can open a file and its member are:
Parameters | Description |
---|---|
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. |
FileAccess:
This enumerator has three members:
- Read: Only to read.
- ReadWrite: Both read and write
- Write: Only write.
FileShare:
FileShare refers to sharing of files and its members are:
Parameters | Description |
---|---|
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | using System; using System.IO; namespace FileProgram { class Program { static void Main(string[] args) { FileStream f = new FileStream("sample.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite); for (int i = 97; i <= 122; i++) { f.WriteByte((byte) i); } f.Close(); } } } |
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.
abcdefghijklmnopqrstuvwxyz