A file is a set of data with a particular name and directory path stored on a disc. It becomes a stream when a file is opened for reading or writing.
Basically, the stream is the sequence of bytes flowing through the direction of communication. Two key streams exist: the stream of input and the stream of output. The input stream is used to read file data (read operation) and the output stream (write operation) is used to write to the file.
C # Classes I / O :
There are different classes in the System. IO namespace that are used to perform various file operations, such as creating and removing files, reading from or writing a file, closing a file, etc.
Some widely used non-abstract classes in the System. IO namespace are shown in the following table −
1. BinaryReader :
Reads primitive data from a binary stream.
Writes primitive data in binary format.
A temporary storage for a stream of bytes.
Helps in manipulating a directory structure.
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.
The Class FileStream :
In the System. IO namespace, the FileStream class helps to read from, write to, and close files. This class is derived from Stream, an abstract class.
To build a new file or open an existing file, you need to build a FileStream object. The syntax used to construct a FileStream object is the following:
FileStream <object_name> = new FileStream( <file_name>, <FileMode Enumerator>,
<FileAccess Enumerator>, <FileShare Enumerator>);
For example, we create a FileStream object F for reading a file named sample.txt as shown −
1. FileMode :
The FileMode enumerator defines various methods for opening files. The members of the FileMode enumerator are −
Append − It opens an existing file and puts cursor at the end of file, or creates the file, if the file does not exist.
Create − It creates a new file.
CreateNew − It specifies to the operating system, that it should create a new file.
Open − It opens an existing file.
OpenOrCreate − It specifies to the operating system that it should open a file if it exists, otherwise it should create a new file.
Truncate − It opens an existing file and truncates its size to zero bytes.
2. FileAccess :
FileAccess enumerators have members: Read, ReadWrite and Write.
3. FileShare :
FileShare enumerators have the following members −
Inheritable − It allows a file handle to pass inheritance to the child processes
None − It declines sharing of the current file
Read − It allows opening the file for readin.
ReadWrite − It allows opening the file for reading and writing
Write − It allows opening the file for writing
- Advanced File Operations :
Simple file operations in C # are given by the previous example. However, you need to know the widely used properties and methods of these classes to utilise the tremendous forces of the C # System. IO classes.
1. Reading from and Writing into Text files :
- It involves reading from and writing into text files.
- The StreamReader and StreamWriter class helps to accomplish it.