Reference using system. Text; namespace

Source: Internet
Author: User

Common attributes and methods of filestream:

Attribute:

Canread checks whether the current stream supports reading and returns the bool value. True indicates that the stream can be read.

Canwrite checks whether the current stream supports writing, and returns the bool value. True indicates that the stream can be written.

Method:

Read () reads data from the stream and returns a byte array.

Write () writes the byte block (byte array) into the stream

Seek () sets the start position for file reading or writing.

Flush () clears the stream buffer so that all buffered data is written to the file.

Close () Close the current stream and release all associated system resources

File Access Method: (fileaccess)

There are three enumerations:

Fileaccess. Read (access to files)

Fileaccess. Write (write the object)

Fileaccess. readwrite (for file read or write operations)

File opening mode: (filemode) includes six enumerations

Filemode. append: open an existing file and prepare to append data to the file. It can only be used with fileaccess. Write.

Filemode. Create indicates that the operating system should create a new file. If the file already exists, it will be overwritten.

Filemode. createnew indicates that the operating system should create a new file. If the file already exists, an exception is thrown.

Filemode. Open indicates that the operating system should open an existing file. The ability to open the file depends on the value specified by fileaccess.

Filemode. openorcreate indicates that the operating system should open the file. If the file does not exist, create a new file.

Filemode. truncate indicates that the operating system should open existing files and clear the File Content

File Sharing Method: (fileshare)

Fileshare is used to prevent exceptions when several programs access the same file at the same time.

File sharing methods include:

Fileshare. None decline to share the current file

Fileshare. Read allows other programs to read the current file.

Fileshare. Write allows many other programs to write the current file.

Fileshare. readwrite allows other programs to read and write the current file.

Use the filestream class to create a file stream object:

Filestream (string file path, filemode file opening Mode)

Filestream (string file path, filemode file opening mode, fileaccess File Access Mode)

Filestream (string file path, filemode file opening mode, fileaccess file access mode, fileshare File Sharing Mode)

Example:

// Create a. txt file on the C drive and operate the file using the fs stream object. The FS working mode is new (filemode. Create)

Filestream FS = new filestream (@ "C: \ a.txt", filemode. Create );

// Create a. txt file on the C drive and operate the file using the fs stream object. The FS working mode is the access mode of the new (filemode. Create) file and the write (fileaccess. Write)

Filestream FS = new filestream (@ "C: \ a.txt", filemode. Create, fileaccess. Write );

// Create a. txt file on the C drive and operate the file using the fs stream object. The FS working mode is new (filemode. create) the file access mode is fileaccess. write) the file sharing mode is to decline sharing (fileshare. none)

Filestream FS = new filestream (@ "C: \ a.txt", filemode. Create, fileaccess. Write, fileshare. None );

Use the file class to create objects: (common)

Custom file opening method: file. Open (string, filemode );

Open the file and read it: file. openread (string );

Open the file and write it into it: file. openwrite (string );

Example:

// Create a new 123.txt file on the C drive and use the stream object FS to perform operations on the file. FS can append the file content to the file mode. append

Filestream FS = file. Open (@ "C: \ 123.txt", filemode. append );

// Create a new 123.txt file on the C drive and use the stream object FS to operate the file. FS can read the file. openread ()

Filestream FS = file. openread (@ "C: \ 123.txt ");

// Create a new 123.txt file on the C drive and use the stream object FS to operate the file. FS can perform the write operation file. openwrite ()

Filestream FS = file. openwrite (@ "C: \ 123.txt ");

File example:

Read an object:

// The path generated by the new fs stream object is the value of textbox1.text, and the file mode is filemode. openorcreate (readable and writable)

Using (filestream FS = file. Open (textbox1.text, filemode. openorcreate ))
{

// Create a new byte array. The length of the array is the length of the FS file object (used to store files later)
Byte [] bt = new byte [fs. Length];

// Get the content in the FS object stream through the FS object read method BT
FS. Read (BT, 0, BT. Length );

// Close the fs stream object
FS. Close ();

// Extract the data in the BT byte array by the encoding. Default. getstring (BT) method and deliver it to textbox2.text.
Textbox2.text = system. Text. encoding. Default. getstring (BT );
}

Write a file:

// Create a fs stream object. The object operation file path is in textbox1.text, And the FS operation mode is filemode. Create

Using (filestream FS = file. Open (textbox1.text, filemode. Create ))
{

// Create a new byte array BT object. The BT object obtains the encoding value of textbox2.text.
Byte [] bt = system. Text. encoding. Default. getbytes (textbox2.text );

// Write the value of the BT byte array object to the fs stream object (file)
FS. Write (BT, 0, BT. Length );

// Close the Stream Object
FS. Close ();
}

Note:

No matter how much code you have, you can read and write files in the following three steps:

1. Create a file read/write Stream Object

2. Read and Write files

3. close the file stream

 

 

From http://hi.baidu.com/%D7%B7%C7%F3a%B5%CD%B5%F7

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.