May 11, 2021 C#
A file is a collection of data stored on disk with a specified name and directory path. When you open a file for reading and writing, it becomes a stream.
Essentially, a stream is a sequence of bytes passed through a communication path. T here are two main streams: the input stream and the output stream. The input stream is used to read data from the file (read operation), and the output stream is used to write data (write) to the file.
System.IO namespaces have a variety of different classes for performing a variety of file operations, such as creating and deleting files, reading or writing files, closing files, and so on.
The following table lists some of System.IO classes commonly used in the namespace:
I/O 类 | 描述 |
---|---|
BinaryReader | 从二进制流读取原始数据。 |
BinaryWriter | 以二进制格式写入原始数据。 |
BufferedStream | 字节流的临时存储。 |
Directory | 有助于操作目录结构。 |
DirectoryInfo | 用于对目录执行操作。 |
DriveInfo | 提供驱动器的信息。 |
File | 有助于处理文件。 |
FileInfo | 用于对文件执行操作。 |
FileStream | 用于文件中任何位置的读写。 |
MemoryStream | 用于随机访问存储在内存中的数据流。 |
Path | 对路径信息执行操作。 |
StreamReader | 用于从字节流中读取字符。 |
StreamWriter | 用于向一个流中写入字符。 |
StringReader | 用于读取字符串缓冲区。 |
StringWriter | 用于写入字符串缓冲区。 |
System.IO FileStream class in the namespace helps to read and write files and close them. 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 .txt sample:
FileStream F = new FileStream("sample.txt", FileMode.Open, FileAccess.Read, FileShare.Read);
参数 | 描述 |
---|---|
FileMode |
FileMode 枚举定义了各种打开文件的方法。FileMode 枚举的成员有:
|
FileAccess |
FileAccess 枚举的成员有: Read 、 ReadWrite 和 Write 。 |
FileShare |
FileShare 枚举的成员有:
|
The following program demonstrates the use of the 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 -1
The example above demonstrates the simple file operation in C#. However, to take full advantage of System.IO of classes, you need to know the properties and methods commonly used by those classes.
In the following sections, we'll discuss these classes and what they do. Click the link to learn more about each section:
主题 | 描述 |
---|---|
文本文件的读写 | 它涉及到文本文件的读写。 StreamReader 和 StreamWriter 类有助于完成文本文件的读写。 |
二进制文件的读写 | 它涉及到二进制文件的读写。 BinaryReader 和 BinaryWriter 类有助于完成二进制文件的读写。 |
Windows 文件系统的操作 | 它让 C# 程序员能够浏览并定位 Windows 文件和目录。 |