HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
Afile is a data storage with the specified name and directory path set in the disk.When the file is opened for reading and writing, it becomes astream.
Fundamentally speaking, stream is a sequence of bytes transmitted through a communication path. There are two main streams:input and output streams.Input stream for reading data (read) from the file,the output streamfor writing data to a file (write).
System.IO namespace have a variety of classes for performing various file operations, such as creating and deleting files, reading or writing files, closing files.
The following table lists some of the System.IO namespace commonly used non-abstract class:
I/O 类 | 描述 |
---|---|
BinaryReader | 从二进制流读取原始数据。 |
BinaryWriter | 以二进制格式写入原始数据。 |
BufferedStream | 字节流的临时存储。 |
Directory | 有助于操作目录结构。 |
DirectoryInfo | 用于对目录执行操作。 |
DriveInfo | 提供驱动器的信息。 |
File | 有助于处理文件。 |
FileInfo | 用于对文件执行操作。 |
FileStream | 用于文件中任何位置的读写。 |
MemoryStream | 用于随机访问存储在内存中的数据流。 |
Path | 对路径信息执行操作。 |
StreamReader | 用于从字节流中读取字符。 |
StreamWriter | 用于向一个流中写入字符。 |
StringReader | 用于读取字符串缓冲区。 |
StringWriter | 用于写入字符串缓冲区。 |
System.IO namespaceFileStream class to read and write and help close the file.This class is derived from the abstract class Stream.
You need to create aFileStream object to create a new file, or open an existing file.Creates aFileStream object syntax is as follows:
FileStream <object_name> = new FileStream (<file_name>, <FileMode Enumerator>, <FileAccess Enumerator>, <FileShare Enumerator>);
For example, create a FileStream object to read the file namedF sample.txtof:
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 theFileStream class usage:
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,234,567,891,011,121,314 151617181920-1
The above example demonstrates a simple C # file operations. However, to take full advantage of C # System.IO class of powerful, you need to know these common classes of properties and methods.
In the following sections, we will discuss these classes and the operations they perform. Click on the link to learn more about the various parts of knowledge:
主题 | 描述 |
---|---|
文本文件的读写 | 它涉及到文本文件的读写。StreamReader和StreamWriter类有助于完成文本文件的读写。 |
二进制文件的读写 | 它涉及到二进制文件的读写。BinaryReader和BinaryWriter类有助于完成二进制文件的读写。 |
Windows 文件系统的操作 | 它让 C# 程序员能够浏览并定位 Windows 文件和目录。 |