C # file read and write supplemental knowledge

Source: Internet
Author: User
Tags bool

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:

of the file as an append
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

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);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.