C# file input and output
A file is a collection of data stored on disk with a specified name and directory path. When a file is opened for reading or writing, it becomes a stream.
Fundamentally, a stream is a sequence of bytes passed over a communication path. There are two main streams: Input stream and Output stream. Input stream is used to read data from the file (read operation), Output stream is used to write data to the file (write operation).
C# I/O Classes
The System.IO namespace has various classes that are used to perform various file operations such as creating and deleting files, reading or writing Import files, close files, etc.
The following table lists some commonly used non-abstract classes in the System.IO namespace:
I/O Class | Description |
---|---|
BinaryReader | Read raw data from a binary stream. |
BinaryWriter | Write raw data in binary format. |
BufferedStream | Temporary storage of byte stream. |
Directory | Helps in manipulating the directory structure. |
DirectoryInfo | is used to perform operations on the directory. |
DriveInfo | Provides drive information. |
File | Helps with working with files. |
FileInfo | is used to perform operations on files. |
FileStream | is used for reading and writing anywhere in the file. |
MemoryStream | Used for random access to a stream of data stored in memory. |
Path | Perform operations on path information. |
StreamReader | is used to read characters from a byte stream. |
StreamWriter | is used to write characters to a stream. |
StringReader | is used to read the string buffer. |
StringWriter | is used to write string buffer. |
FileStream Class
The FileStream class in the System.IO namespace facilitates the reading, writing, and closing of files. This class is derived from the abstract class Stream.
You need to create a FileStream object to create a new file, or open an existing file. The syntax for creating a FileStream object is as follows:
FileStream <object_name> = new FileStream( <file_name>, <FileMode Enumerator>, <FileAccess Enumerator>, <FileShare Enumerator>);
For example, create a FileStream object F to read a file named sample.txt:
FileStream F = new FileStream("sample.txt", FileMode.Open, FileAccess.Read, FileShare.Read);
Parameter | Description |
---|---|
FileMode | FileMode The enumeration defines various methods of opening files. The members of the FileMode enumeration are:
|
##FileAccess The members of the enumeration are: Read, ReadWrite and Write. | |
FileShare The members of the enumeration are:
|
FileStream class:
using System; using System.IO; namespace FileIOApplication { class Program { static void Main(string[] args) { FileStream F = new FileStream("test.dat", FileMode.OpenOrCreate, FileAccess.ReadWrite); for (int i = 1; i <= 20; i++) { F.WriteByte((byte)i); } F.Position = 0; for (int i = 0; i <= 20; i++) { Console.Write(F.ReadByte() + " "); } F.Close(); Console.ReadKey(); } } }When the above code is compiled and executed, it produces the following results:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 -1C# Advanced file operationsThe above example demonstrates simple file operations in C#. However, to take full advantage of the power of the C# System.IO classes, you need to know the properties and methods commonly used by these classes. In the following sections, we will discuss these classes and the operations they perform. Please click on the links to learn more about each section:
Description | |
---|---|
It involves reading and writing text files. The | StreamReader and StreamWriter classes help in reading and writing text files. |
It involves reading and writing of binary files. The | BinaryReader and BinaryWriter classes help in reading and writing binary files. |
It enables C# programmers to browse and locate Windows files and directories. |