A using System.IO is referenced when the convection is operated; Namespaces
FileStream commonly used properties and methods:
Property:
CanRead determines whether the current stream supports reading, returns a bool value, and True indicates that it can read
CanWrite determines whether the current stream supports writing, returns a bool value, and True indicates that it can be written to
Method:
Read () reads data from the stream, returns an array of bytes
Write () writes a byte block (byte array) to the stream
Seek () set the starting position of the file read or write
Flush () clears the stream buffer so that all buffered data is written to the file
Close () closes the current stream and releases all system resources associated with it
How files are accessed: (FileAccess)
Includes three enumerations:
FileAccess.Read (Read access to files)
FileAccess.Write (write to file)
FileAccess.ReadWrite (read or write to file)
File open mode: (FileMode) includes 6 enumerations
Filemode.append Open an existing file to append data to the file and use it only with FileAccess.Write
FileMode.Create indicates that the operating system should create a new file, and it will be overwritten if the file already exists
FileMode.CreateNew indicates that the operating system should create a new file and throws an exception if the file already exists
FileMode.Open indicates that the operating system should open an existing file and that the ability to open depends on the value specified by FileAccess
FileMode.OpenOrCreate indicates that the operating system should open the file and create a new file if it does not exist
Filemode.truncate indicates that the operating system should open an existing file and empty the contents of the file
File sharing mode: (FileShare)
The fileshare approach is to prevent several programs from accessing the same file at the same time causing an exception.
File sharing includes four ways:
Fileshare.none declined to share the current file
FileShare.Read allows other programs to read the current file
Fileshare.write is allowed to write the current file with another program
Fileshare.readwrite allows other programs to read and write the current file
To create a file stream object using the FileStream class:
FileStream (String file path, FileMode file open mode)
FileStream (String file path, FileMode file open mode, FileAccess file access)
FileStream (String file path, FileMode file open mode, FileAccess file access method, FileShare file sharing)
Cases:
Create A.txt file in C disk, use FS stream object to operate the file, the working mode of FS is new (FileMode.Create)
FileStream fs=new FileStream (@ "C:a.txt", FileMode.Create);
Create A.txt file in C disk, use FS stream object to manipulate file, FS mode is the access mode of new (filemode.create) file is write (FileAccess.Write)
FileStream fs=new FileStream (@ "C:a.txt", filemode.create,fileaccess.write);
Create a A.txt file in C disk, use the FS stream object to manipulate the file, and the FS mode is that the access mode of the new (FileMode.Create) file is the shared mode of the write (FileAccess.Write) file is declined to share ( Fileshare.none)
FileStream fs=new FileStream (@ "C:a.txt", Filemode.create,fileaccess.write,fileshare.none);
To create an object using the file class: (Common)
Customize how the file is opened: File.Open (String,filemode);
Open file for reading: File.openread (String);
Open file for writing: File.openwrite (String);
Examples are as follows:
In c disk new 123.txt file, using stream object FS to operate the file, FS can row file content append operation Filemode.append
FileStream Fs=file.open (@ "C:123.txt", filemode.append);
New 123.txt file in C disk, use stream object FS to operate the file, FS can read file File.openread ()
FileStream Fs=file.openread (@ "C:123.txt");
New 123.txt file in C disk, use stream object FS to operate the file, FS can write operation File.openwrite ()
FileStream fs=file.openwrite (@ "C:123.txt");
Use the file example:
To read a file:
The path generated by the new FS Stream object object is the TextBox1.Text value, and the file pattern is filemode.openorcreate (readable and writable)
using (FileStream fs = File.Open (TextBox1.Text, FileMode.OpenOrCreate))
{
Creates a new byte array whose length is the length of the fs file object (after which the file is stored)
Byte[] Bt=new Byte[fs. Length];
The content of FS object stream is obtained by the Read method of FS object
Fs. Read (BT,0,BT. Length);
Turn off FS Stream object
Fs. Close ();
The data in the BT byte array is removed from the Encoding.Default.GetString (BT) method and handed to the TextBox2.Text
TextBox2.Text = System.Text.Encoding.Default.GetString (BT);
}
To write to a file:
New fs Stream object, the file path of object operation in TextBox1.Text, the mode of operation of FS is FileMode.Create
using (FileStream fs = File.Open (TextBox1.Text, FileMode.Create))
{
New byte array bt object, the BT object gets the value of the TextBox2.Text encoding
byte[] bt = System.Text.Encoding.Default.GetBytes (TextBox2.Text);
Writes the value of a BT byte array object to the FS stream object (file)
Fs. Write (BT,0,BT. Length);
Close Stream Object
Fs. Close ();
}
Note:
A lot of reading and writing about files no matter how much the code has, it is the following three steps:
1. Create a file read-write Stream object
2. Read and write to the document
3. Close file stream