C# File input and output
aFileIs a data collection stored on disk with a specified name and directory path. When a file is opened for reading and writing, it becomes aflow。
Fundamentally, a stream is a sequence of bytes transmitted through a communication path. There are two main streams:input streamandoutput stream。input streamUsed to read data from a file (read operations),output streamUsed to write data to a file (write operations).
C# I/O classes
The System.IO namespace has various classes for performing various file operations, such as creating and deleting files, reading or writing files, closing files, and so on.
The following table lists some commonly used non-abstract classes in the System.IO namespace:
| I/O class | Description |
|---|---|
| BinaryReader | Reads raw data from a binary stream. |
| BinaryWriter | Writes raw data in binary format. |
| BufferedStream | Temporary storage for byte streams. |
| Directory | Helps with directory structure operations. |
| DirectoryInfo | Used to perform operations on directories. |
| DriveInfo | Provides information about drives. |
| File | Helps in processing files. |
| FileInfo | Used to perform operations on files. |
| FileStream | Used for reading and writing at any position in a file. |
| MemoryStream | Used for random access to data streams stored in memory. |
| Path | Performs operations on path information. |
| StreamReader | Used to read characters from a byte stream. |
| StreamWriter | Used to write characters to a stream. |
| StringReader | Used to read a string buffer. |
| StringWriter | Used to write a string buffer. |
FileStream class
In the System.IO namespace,FileStreamThe class facilitates reading, writing, and closing of files. This class is derived from the abstract class Stream.
You need to create aFileStreamobject to create a new file, or open an existing file. CreatingFileStreamThe syntax of the object is as follows:
FileStream <object_name> = new FileStream( <file_name>, <FileMode Enumerator>, <FileAccess Enumerator>, <FileShare Enumerator>);
For example, create a FileStream objectFto read a file namedsample.txtFile:
FileStream F = new FileStream("sample.txt", FileMode.Open, FileAccess.Read, FileShare.Read);
| Parameter | Description |
|---|---|
| FileMode | FileModeThe enumeration defines various methods of opening files. The members of the FileMode enumeration are:
|
| FileAccess | FileAccessThe members of the enumeration are:Read、ReadWriteandWrite。 |
| FileShare | FileShareThe members of the enumeration are:
|
Example
The following program demonstratesFileStreamClass usage:
Example
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 -1
C# Advanced File Operations
The above examples demonstrate simple file operations in C#. However, to fully utilize the powerful features of the C# System.IO classes, you need to know the commonly used properties and methods of these classes.
In the following sections, we will discuss these classes and the operations they perform. Please click the links to learn more about each part:
| topic | Description |
|---|---|
| Reading and Writing Text Files | It involves reading and writing text files.StreamReaderandStreamWriterThe class helps in completing the reading and writing of text files. |
| Reading and writing binary files | It involves reading and writing binary files.BinaryReaderandBinaryWriterThe class facilitates reading and writing of binary files. |
| Windows file system operations | It allows C# programmers to browse and locate Windows files and directories. |