(3) file system I/O
For the operating system, all files are byte oriented, and disk files are the most common file types. C # provides the method to read and write files in bytes. The most common method is to read and write files using byte data streams, of course, we can also wrap these byte data streams in a character-based object and use the character data stream to read and write files.
To create a byte data stream appended to a file, we can use the filestream class. Because the filestream class is derived from the byte data stream class of the stream class, for the operating system, files are byte-oriented, therefore, you can use the filestream class to read and write all files, such as executable files, target files, and source files.
To read and write files, we must first know how to open and close a file. The filestream class defines multiple constructor types. Commonly Used constructor types include:
Filestream (string path, filemode mode );
Filestream (string path, filemode mode, fileaccess );
Filestream (string path, filemode mode, fileaccess access, fileshare share );
The path specifies the complete path of the file, the filemode is Enumeration type, the file creation mode is specified, the fileaccess is Enumeration type, the file read and write permissions are specified, and the fileshare is Enumeration type, specifies the file sharing attribute. The following describes in detail the meaning of each Enumeration type and its value.
Filemode enumeration Value
Value |
Description |
Filemode. append |
Append the output to the end of the file |
Filemode. Open |
Open an existing file |
Filemode. openorcreate |
Open a file. If it does not exist, create a new file. |
Filemode. Create |
Create a new output file. An existing file with the same name will be destroyed. |
Filemode. createnew |
Create a new output file that does not have the same name as an existing file |
Filemode. truncate |
Open an existing file and reduce the file length to 0. |
Fileaccess enumeration Value
Value |
Description |
Fileaccess. Read |
Readable attributes |
Fileaccess. Write |
Writable attributes |
Fileaccess. readwrite |
Read/write attributes |
Fileshare enumeration Value
Value |
Description |
Delete |
Allow subsequent File Deletion |
Inheritable |
The file handle can be inherited by sub-processes. Win32 does not support this function directly. |
None |
Decline the sharing of the current file. Before the file is closed, any request to open the file (this process or another process) will fail. |
Read |
Allow subsequent file reading. If this flag is not specified, all requests that open the file for reading will fail before the file is closed. |
Write |
Allow subsequent file opening and writing. If this flag is not specified, all requests that open the file for writing will fail before the file is closed. |
Readwrite |
Allows reading or writing a file to be opened later. If this flag is not specified, requests for reading or writing the file will fail before the file is closed. |
When opening a file, an error may occur, and an exception will be thrown. Common exceptions include the ioexception class (including pathtoolongexception, directorynotfoundexception, and filenotfoundexception ), argumentnullexception (the file name is blank), argumentexception (the file name is invalid), argumentoutofrangeexception (the mode is invalid), and securityexception (the user does not have access permissions ).
When we use a file, we need to close it. In this case, we need to call the close function. This function releases the system resources allocated to the file. It actually works by calling the dispose () method, the dispose () method actually releases resources.