C # (input and output streams)

Source: Internet
Author: User

I. system. Io namespace Of the. NET Framework class library

The system. Io namespace contains the types that allow reading and writing files and data streams, as well as the types that support basic files and directories.

II. C # filestream for file read/write

 
// Open a file directly using the filestream class

 
Filestream fs1 = file. Open ("C: // test.txt", Filemode. Open );

 
 

 
// Open the file "C: // test.txt" in append mode and write some content to "C: // test.txt"

 
Filestream fs2 = file. Open ("C: // test.txt", Filemode. append, fileaccess. Write );

 
 

// Open the file and clear the content before performing operations on the file

 
Filestream fs3 = file. Open ("C: // test.txt", Filemode. truncate, fileaccess. readwrite, fileshare. Read );

 
 

 
// This method creates a readable object and allows others to read the object content.

 
Filestream myfilestream1 =NewFilestream (@ "C:/testing.txt", Filemode. Create );

 

3 C # stream-based input/output
C # stream-based Input and Output.: Stream-connects to physical devices through the C # I/O system, that is, physical storage devices such as hard disks that are read and written at ordinary times. stream/stream methods and attributes include:

Void close () Close stream
Void flush () Clean content in the stream
Int readbyte () Returns an integer representing the number of input bytes. If no data is returned,-1 is returned.
Int read (byte [] Buf, int offset, int numbytes) Reads numbytes bytes to the offset-based start position of byte [], and returns the number of successfully read bytes.
Long seek (long offset, seekorigin origin) Locate the current position to the offset after the origin is the initial position
Void writebyte (byte B) Write a single byte to an output stream
Void write (byte [] Buf, int offset, int numbytes) Write byte [] Buf numbytes starting from offset
Bool Canread Readable or not
Bool canseek Addressing supported?
Bool canwrite Whether data can be written
Long length Stream Length
Long position Current stream position

4-stream inheritance Structure

Stream is a large class. When reading and writing files, you can perform professional Data Reading and Writing through different streams.

 

Attributes and meanings of filemode

Filemode. Create Create a file. A file with the same name will be destroyed.
Filemode. createnew Create a new file, which does not exist before
Filemode. Open Open an existing file
Filemode. openorcreate Open the file if it exists. Otherwise, create a new file.
Filemode. truncate Open an existing file and clear its content
Filemode. append Write Data to the end of the file in the form of append

Attributes and definitions of fileaccess

fileaccess. Read Read files
fileaccess. Write write files
fileaccess. readwrite read/write files
Related Article

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.