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 transmitted along a communication path. There are two main streams:Input stream and Output stream。Input streamis used to read data from a file (read operation),Output streamis used to write data to a file (write operation).
C# I/O Classes
The System.IO namespace has a variety of 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 common non-abstract classes in the System.IO namespace:
| I/O Classes | Description |
|---|---|
| BinaryReader | Reads raw data from a binary stream. |
| BinaryWriter | Writes raw data in binary format. |
| BufferedStream | Temporary storage of a byte stream. |
| Directory | Helps manipulate directory structures. |
| DirectoryInfo | Used to perform operations on directories. |
| DriveInfo | Provides information about drives. |
| File | Helps handle files. |
| FileInfo | Used to perform operations on files. |
| FileStream | Used for reading from and writing to any position in a file. |
| MemoryStream | Used for random access to a data stream 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 from a string buffer. |
| StringWriter | Used to write to a string buffer. |
FileStream Class
In the System.IO namespace, the FileStream class helps with reading, writing, and closing files. This class derives from the abstract class Stream.
You need to create a FileStream object to create a new file, or to open an existing file. The syntax for creating a FileStream object is as follows:
FileStream = new FileStream( ,
, , );
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);
| Parameters | Description |
|---|---|
| FileMode |
FileMode The enumeration defines various ways to open a file. The members of the FileMode enumeration are: |
FileAccess The members of the enumeration are:Read、ReadWrite and Write。
| | FileShare |FileShare The members of the enumeration are:
- Inheritable: Allows the file handle to be inherited by a child process. Win32 does not directly support this feature.
- None: Declines to share the current file. Any request to open the file (issued by this process or another process) will fail until the file is closed.
- Read: Allows the file to be subsequently opened for reading. If this flag is not specified, any request to open the file for reading (issued by this process or another process) will fail until the file is closed. However, even if this flag is specified, additional permissions may still be required to access the file.
- ReadWrite: Allows the file to be subsequently opened for reading or writing. If this flag is not specified, any request to open the file for reading or writing (issued by this process or another process) will fail until the file is closed. However, even if this flag is specified, additional permissions may still be required to access the file.
- Write: Allows the file to be subsequently opened for writing. If this flag is not specified, any request to open the file for writing (issued by this process or another process) will fail until the file is closed. However, even if this flag is specified, additional permissions may still be required to access the file.
- Delete: Allows the file to be subsequently deleted.
Example
The following program demonstrates the use of the FileStream class:
Example
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 code above is compiled and executed, it produces the following result:
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 example above demonstrates simple file operations in C#. However, to fully leverage 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. Click the links to learn more about each section:
| Topic | Description |
|---|---|
| Reading and Writing Text Files | It involves reading and writing text files.StreamReader and StreamWriter class helps with reading and writing text files. |
| Reading and Writing Binary Files | It involves reading and writing binary files.BinaryReader and BinaryWriter class helps with reading and writing binary files. |
| Windows File System Operations | It enables C# programmers to browse and locate Windows files and directories. |