Summary of common FileStream properties and methods
- 2020-06-03 08:08:30
- OfStack
using System. IO is used when convection is operated; The namespace
Common FileStream properties and methods:
Properties:
CanRead determines whether the current stream supports reading and returns the value bool, which means it can be read
CanWrite determines whether the current stream supports writing and returns the value bool, indicating that it can be written
Methods:
Read() reads data from the stream and returns an array of bytes
Write() writes blocks of bytes (byte arrays) to the stream
Seek() sets the start of a file read or write
Flush() clears the stream buffer so that all cached data is written to the file
Close() closes the current flow and releases all system resources associated with it
How to access files :(FileAccess)
Includes 3 enumerations:
FileAccess.Read (read access to files)
FileAccess.Write (write to file)
FileAccess. ReadWrite (read or write to a file)
File open mode :(FileMode) contains six enumerations
FileMode.Append opens an existing file to append data to the file. It can only be used with ES60en.es61EN1
FileMode. Create indicates that the operating system should create a new file, which will be overwritten if it already exists
FileMode.CreateNew indicates that the operating system should create a new file and will throw an exception if the file already exists
FileMode.Open indicates that the operating system should open an existing file, 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 one does not exist
FileMode.Truncate instructs the operating system to open an existing file and empty the file contents
File sharing method :(FileShare)
FileShare is used to avoid the situation where several programs accessing the same file at the same time can cause an exception.
There are four ways of file sharing:
FileShare.None declined to share the current file
FileShare.Read allows other programs to read the current file
FileShare.Write allows other programs to write the current file
FileShare.ReadWrite allows other programs to read and write current files
Create file flow objects using the FileStream class:
FileStream (String file path, FileMode file open mode)
FileStream (String file path, FileMode file open mode, FileAccess file access mode)
FileStream (String file path, FileMode file open mode, FileAccess file access mode, FileShare file sharing mode)
Ex. :
// Create a. txt file on C disk, use fs stream object to operate the file, fs work mode is new (ES137en. Create)
FileStream fs=new FileStream(@"c:\a.txt",FileMode.Create);
// Create a. txt file on C disk, use fs stream object to operate the file, fs working mode is to create (ES149en. Create) file access mode is to write (ES151en. Write)
FileStream fs=new FileStream(@"c:\a.txt",FileMode.Create,FileAccess.Write);
// Create a.txt file in C disk, use fs stream object to operate the file, fs mode is to create (ES163en.Create) file access mode is to write (ES165en.Write) file share mode is to refuse sharing (ES167en.None)
FileStream fs=new FileStream(@"c:\a.txt",FileMode.Create,FileAccess.Write,FileShare.None);
Use the File class to create objects :(commonly used)
Customize the way to open files: ES177en.Open (String, FileMode);
Open the file for reading: File.OpenRead(String);
Open the file and write: File.OpenWrite(String);
Examples are as follows:
// Create a new 123.txt file on the C disk. Use the stream object fs to operate the file. fs can append the file to the content of the file
FileStream fs=File.Open(@"c:\123.txt",FileMode.Append);
// Create a new 123.txt file in C disk, use the stream object fs to operate the file, and fs can read the file ES210en.OpenRead ()
FileStream fs=File.OpenRead(@"c:\123.txt");
// Create a new 123.txt file on C disk, use the flow object fs to operate the file, and fs can write File. OpenWrite()
FileStream fs=File.OpenWrite(@"c:\123.txt");
Example using File:
Read the file:
// New fs stream object The path generated by the object is the value of ES234en1.text and the mode of the file is ES236en.OpenOrCreate (readable and writable)
using (FileStream fs = File.Open(textBox1.Text, FileMode.OpenOrCreate))
{
// Create a new byte array with the length of the fs file object (to hold the file later)
byte[] bt=new byte[fs.Length];
// The contents of the fs object flow are obtained through the Read method bt of the fs object
fs.Read(bt,0,bt.Length);
// Close the fs stream object
fs.Close();
// The data in the bt byte array is retrieved by the Encoding.Default.GetString (bt) method and handed over to ES268en2.text
textBox2.Text = System.Text.Encoding.Default.GetString(bt);
}
Write to the file:
// Create a new fs stream object. The file path of the object operation is in ES279en1.text. The operation mode of fs is ES282en.Create
using (FileStream fs = File.Open(textBox1.Text, FileMode.Create))
{
// New byte array bt object, bt object gets the value of textbox2.text Encoding
byte[] bt = System.Text.Encoding.Default.GetBytes(textBox2.Text);
// Writes the value of the bt byte array object to the fs stream object (file)
fs.Write(bt,0,bt.Length);
// Close the flow object
fs.Close();
}
Note:
No matter how much code there is to read and write a file, there are three steps:
1. Create a file read-write stream object
2. Read and write files
Close the file flow