Read and write text files
Reading and writing a text file using C # requires 5 steps:
1. Create a file stream
2. Create a reader or writer
3. Perform read and write operations
4. Close the reader or the writer
5. Close the file stream
Cases
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 usingSystem.IO;//Introducing Namespaces7 8 namespaceDemo9 {Ten class Program One { A Static voidMain (string[] args) - { - //Create a file stream theFileStream stream =NewFileStream ("FilePath", FileMode.Create); - //Creating a writer -StreamWriter writer =NewStreamWriter (stream); - //Write Content +Writer. Write ("Write"); - //Close the writer + writer. Close (); A //Close File Stream at Stream. Close (); - } - } -}
The FileMode in FileStream refers to the pattern of open files, which can be understood by literal means, creating creates a file.
Reading a file only converts StreamWriter to StreamReader. The rest can be replaced by the words into English.
Create StreamWriter generally have two parameters, one is the FileStream object, one is the encoding character encoding, commonly used is the default. Solve garbled problems.
File operations
File class: (Static)
Exists (string path) is used to check whether the specified file exists, has a return value, and a bool type.
Copy (string sourcefilepath,string Destinationfilepath) copies the contents of the source file of the specified path to the destination file, and creates a new file in the specified path if the destination file does not exist.
Move (string sourcefilepath,string destinationfilepath) moves the specified file to a new path.
Delete (string path) deletes the specified file and does not throw an exception if the specified file does not exist.
FileInfo class: (instance)
Property:
Exists: Used to check if the specified file exists and returns a Boolean value.
Extension: Gets the string that represents the file name extension part.
Name: Gets the file name.
FullName: Gets the full directory of the directory or file.
Method:
CopyTo (string) copies an existing file to a new file and does not allow overwriting an existing file.
Delete () permanently deletes the file.
MoveTo (String) moves the specified file to the new location (string).
Folder operations
Directory class: (Static)
Exists (string path) is used to check whether the specified folder exists on disk.
Move (string sourcedirname,string destdirname) is used to move a file or directory and its contents to a new location.
Delete (string Path,bool recursive) deletes the specified directory, and if the recursive value is true, all directory contents in the subdirectory are deleted.
DirectoryInfo class: (instance)
method is consistent with the methods of the Directory class.
GetDirectories (): Returns an array of subdirectory objects for the current directory, and the directory class has this method, but the return value is an array of names of subdirectories of the current directory.
GetFiles (): This method returns a list of files in the current directory (an array of FileInfo objects). The Directory class also has this method, but the return value is an array of file names under the specified directory.
Differences between manipulating files using static methods and instance methods
With static methods, security checks are performed every time, and if you want to use a file object multiple times, you can use an instance method without having to do a security check every time.
manipulating files using C #