File Operations, file operations
1. Using the C # program to read and write a file requires the following five basic content:
① Create a file stream
② Create a reader or writer
③ Perform read/write operations
④ Close the reader or writer
⑤ Close the file stream
2. using System. IO: This namespace is used to process files and file streams.
3. Create: Create a file with the specified name. If the file exists, rewrite the old file.
CreateNew: Create a new file. If the file exists, an exception occurs, prompting that the file already exists.
Open: Open a file. When this enumeration value is used, the specified file must exist; otherwise, an exception occurs.
OpenOrCreate: OpenOrCreate is similar to the Open member, but if the file does not exist, create a file with the specified name and Open it.
Append: open an existing file and Append content to the end of the file.
4. close the file stream
Close myfs. Close ();
5. StreamWrite Writer:
StreamWrite. Write ():Used to write a stream. This stream is the created stream.
StreamWrite. WriteLine (): used to write a row of data, which is followed by a line break.
StreamWrite. Close (): used to Close the writer.
6. StreamReader
StreamReader is mainly used to read data in a stream. It mainly includes the following methods:
StreamReader. ReadLine (): reads a row of data in the file stream and returns a string
StreamReader. ReadToEnd (): reads the end from the current position and returns a string.
StreamReader. Close (): used to Close the reader
7. The Encoding class is located in the System. Text namespace and used to indicate character Encoding.
① The Encoding format can be specified through static members of the Encoding class. For example:
Encoding. UTF8: gets the Encoding in UTF-8 format.
Encoding. Default: Get the current Encoding of the Operating System
② You can also use the static method GetEncoding (string name) of the Encoding class to specify the character Encoding. The parameter name must be the supported Encoding name of C #. For example:
StreamReader mySr = new StreamReader (myfs, Encoding. GetEncoding ("GB520 "));
8. File and Directory
9. Differences between static fish and non-static fish
------------------------------------- | ---------------------------------------------------
Static | non-static
------------------------------------ | ----------------------------------------------------
Use static modifier | do not use static Modifier
------------------------------------- | ---------------------------------------------------
Contains only static members | can contain static members.
------------------------------------- | ----------------------------------------------------
It cannot contain instance members. | it can contain instance members.
------------------------------------- | ----------------------------------------------------
Call static members by class name | call non-static members by Instance Object
------------------------------------- | ----------------------------------------------------
Cannot be instantiated | can be instantiated
------------------------------------- | -----------------------------------------------------
The instance constructor cannot be included. | the instance constructor is included.
------------------------------------- | -----------------------------------------------------
10. attributes and methods of the FileInfo class
Attribute
Exists: used to check whether the specified file Exists. A boolean value is returned.
Extension: gets a string that represents the file Extension.
Name: get the file Name
FullName: Obtain the complete directory or file
Return Value Type Method
FileInfo CopyTo (string): Copies an existing file to a new file. The existing file cannot be overwritten.
Void Delete (): Permanently deletes an object.
Void MoveTo (string): Move the specified file to a new location (string)
11. Two important methods of the DirectoryInfo class:
① GetDirectories (): This method returns an array of subdirectory objects in the current directory.
② GetFiles (): This method returns the current directory and file list (FileInfo object array ).
12.