I. FileStream detailed
1. (FileStream FS1 = File.Open ("C:\\Test.txt", FileMode.Open));
FileMode.Open directly with FileStream class to open File C:\\Test.txt ",
2. (FileStream FS2 = File.Open ("C:\\Test.txt", Filemode.append, FileAccess.Write));
Filemode.append, open the file "C:\\Test.txt" in an additional way, and write something to "C:\\Test.txt".
3. (FileStream fs3 =file.open ("C:\\Test.txt", Filemode.truncate, FileAccess.ReadWrite, FileShare.Read)).
Filemode.truncate means to open the file to empty the contents of the file after the operation.
4. FileStream MyFileStream1 = new FileStream (@ "C:\Testing.txt", FileMode.Create);
This method means creating a file that can read and write and allowing others to read the contents of the file.
Two. C # based on streaming input and output.
C # is based on the input and output of the stream.: stream-through C # I/O system and physical devices, which is usually read and written hard disk and other physical storage equipment. The methods and properties of the stream/stream are:
Method/properties |
Describe |
void Close () |
Close the stream |
void Flush () |
Clean up the content in the stream |
int ReadByte () |
Returns an integer representing the number of bytes entered, if no data returns-1 |
int Read (byte[] buf,int offset, int numbytes) |
NumBytes bytes read into byte[] at offset, starting position, returns the number of bytes read successfully |
Long Seek (long offset,seekorigin origin) |
Position the current position to the offset at the beginning of the Origin position. |
void WriteByte (Byte b) |
Writes a single byte to an output stream. |
void Write (byte[] buf,int offset, int numbytes) |
Writes the numbytes byte in byte[] buf starting with offset. |
BOOL CanRead |
is readable |
BOOL CanSeek |
Whether to support addressing |
BOOL CanWrite |
Whether data can be written |
Long Length |
The length of the stream |
Long Position |
The current position of the stream. |
Three. The inheritance structure of the stream
Stream is a very large class, when reading and writing files, you can through different streams for professional data reading and writing.
Several rules of the FileMode and FileAccess:
meaning |
filemode.create |
|
Create new file, this file does not exist before |
Open files that already exist |
Open file If present, create new file |
Open with the existing file, clear its contents out of the |
writes data to the last |
of the file as an append
If you want to restrict file access when you open a file, you can do the following construction method:
FileStream (string filename, FileMode mode, FileAccess access);
FileName File mode operation mode
Access can be one of the following values:
Fileaccess.read/fileaccess.write/fileaccess.readwrite;
Filestreamfs=new FileStream ("C:\\tab.txt", Filemode.openorcreate,fileaccess.read);