C #-stream and file (Stream & file) (1)

Source: Internet
Author: User

 

    • Stream Concept

 

Abstract concepts of the lost byte sequence, such as file operations, input/output devices, and internal communication pipelines. The stream class and its derived classes provide general views of these different types of input and output, suchProgramYou do not have to be familiar with the specific details of the operating system and basic equipment, you can also perform operations on the stream.

The following are common operations:

1. Read stream: data transmission from the operation to the Data Structure

2. Write stream: data transmission from the data structure to the stream during this operation

3. Stream search: queries and modifies the current position in the stream.

The preceding three functions can be set through the Canread, canwrite, and canseek attributes.

 

The read and write methods support reading and writing data in different formats. You can use the seek and setlength methods and the length and position attributes to query and modify the current position and length of a stream.

Some streams implement local buffering of execution data to improve performance. For such a stream, the flush method can be used to clear the internal buffer and ensure that all data is written to the data source or storage.

When implementing the stream derived class, you must provide the Implementation of the read and write methods. The asynchronous Methods beginread, endread, beginwrite and endwrite are implemented through the synchronous Methods read and write. Readbyte and writebyte can create a new cell byte array, and then call read and write implementation.

 

 

    • File Overview
A file is a stream, that is, a stream is a series of bytes read from some input.

Files can be divided into text files and binary files by information encoding in external memory.

 

The stream class is system. A class in the IO namespace, in the system. the I/O namespace contains all classes that allow synchronous and asynchronous reading and writing of data streams and files. The following describes common classes.

1. Directory class: contains static methods for all operation directories, such as creating, moving, copying, and deleting directories.

2. directoryinfo class: contains the instance methods of all operation directories, such as various properties of the directory (name, creation time), various operations of the directory (Directory Creation, existence, movement, delete)

3. File class: it is a typical object operation. It provides static methods such as text creation, opening, copying, deletion, and moving. It can also be used to obtain the basic information of the file and set the file.

4. fileinfo class: provides methods for creating, opening, copying, deleting, and moving text for typical file operations. When the file needs to be reused multiple times, use the instance method provided by the fileinfo class. You cannot use the static method provided by file.

5. filestream class: This class provides the ability to read, write, open, and close files. It supports random access to files and can be synchronized to open files for reading and writing, you can also open the file asynchronously for reading and writing.

6. Path class: This class operates string instances that contain file or directory path information. These operations can be performed across platforms.

7. memorystream class: This class creates a stream that supports memory storage.

8. streamreader class: This class can read the content of standard text files. Implements a textreader. The default encoding format is UTF-8.

9. streamwriter class: This class can write content to standard text files. Implements a textwriter. The default encoding format is UTF-8.

10. stringreader class: This class implements the textreader for reading from strings.

11. stringwriter class: This class writes information to a string, which is stored in the basic stringbuilder.

12. textreader class: This class indicates the reader that can read the continuous character system.

13. textwriter class: This class indicates that you can compile an ordered character Series Editor, which is an abstract class.

 

 CodeExample: File Creation and input/output of File Content

 Code

1 If (File. exists (filepath ))
2 {
3 File. Delete (filepath );
4 }
5 Filestream FS = File. Create (filepath, 1024 ); // Create a file
6 Byte [] info = New Utf8encoding ( True ). Getbytes ( " Test content " );
7 FS. Write (info, 0 , Info. Length ); // Write content to the newly created File
8 FS. Close ();
9
10 Using (Streamreader SR = File. opentext (filepath ))
11 {
12 While (Sr. Readline () ! = Null )
13 MessageBox. Show (Sr. Readline ());
14 }

 

Reference code:

Get and set file attributes:/files/long-gengyun/fileattribute.rar

Show files and subfolders in the folder:/files/long-gengyun/getdirectory.rar

Print/files/long-gengyun/fileprint.rar

Computer System Information Retrieval:/files/long-gengyun/getsysteminfo.rar

 

 

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.