Learning from I/O in. Net-the first article I/O)

Source: Internet
Author: User

Article 1I/O

For a completeProgram, Must include the processing of data. This includes user input and data file access. Because of this, the input and output technologies in the program are particularly important.

For a programmer from C/C ++, file input and output are very troublesome. Even if the cache technology is used in I/OCodeIn terms of complexity or program running speed, it is a headache for programmers.

However, since the emergence of Java, this problem seems much easier. Sun used the decorator mode to design I/O when designing Java. Programmers can easily make their I/O Code have the functions they need through object-oriented features. However, Java's I/O package Java is not the perfect solution. io is designed based on the Unix/Linux System for file management (in Unix/Linux, both data files, directories, and hardware devices are managed in the form of files., there is only one root '/' directory in the file format. To some extent, this management method greatly reduces the complexity and difficulty of management .), This is inconvenient for programmers who are familiar with the Windows file management mode.

Currently, I/O libraries similar to Java are also provided on the. NET platform. In particular, the I/O changes made by the. NET 2.0 library make. net I/O out of the shadow of the copied Java.

Next let's take a look at the. net I/O Library:

1,I/OBasic architecture of the database

(1) In. net, the I/O Library is stored in the mscorlib. dll file, and the namespace is system. Io. It contains five basic abstract classes, or even

Stream: Responsible for stream operations on I/O, which is also the core class of. net I/O. Basically, all I/O operations are inseparable.

TextreaderAnd textwriter: Provides abstract classes for character text operations.

Filesysteminfo: This is a class different from Java in. NET 2.0. It is mainly responsible for the management of file information, not only the file information in the general sense of the Windows operating system, but also the directory. This further enhances operations on file information.

Ioexception: This is the class for handling I/O exceptions in the I/O library.

(2) The following describes the basic inheritance relationships.

A), File operation stream class stream:

Inheritance diagram: Stream

Filestream(Normal file Stream)

Bufferedstream(I strongly recommend using cached file streams .)

Memorystream(Manages a stream in a memory area)

Unmanagedmemorystream(Perform unmanaged stream operations on a memory area)

B), Text class textreader and textwriter:

Inheritance diagram: textreader

Streamreader(Text File stream read Operations)

Stringreader(Class for reading string files)

Textwriter

Streamwriter(Text File stream write operations)

Stringwriter(Write operations on stringbuilder files)

C), Exception ioexception:

Inheritance diagram: ioexception

Directorynotfoundexception(Folder not found exception)

Drivenotfoundexception(Drive not found exception)

Endofstreamexception(Stream end exception)

Fileloadexception(File loading exception)

Filenotfoundexception(File not found exception)

Pathtoolongexception(The path is too long)

D), File information class filesysteminfo:

Inheritance diagram: filesysteminfo

Fileinfo(File Information)

Directoryinfo(Directory information)

E)And other classes:

BinaryreaderAnd binarywriter. This is a class that performs binary operations on a stream. It directly inherits to the object class, but must use a stream to open a file.

FileClass, which provides a large number of useful static methods. This is a method class.

DirectoryClass, including obtaining files under a directory, creating a directory, and deleting a directory.

PathClass.

DriveinfoClass, which is named in info but not inherited from filesysteminfo. This class is also a Windows special class. If Mono is used to develop. net programs running on Linux, it will be meaningless. Its main function is to obtain the drive size, available space, volume label, format, and so on.

(3) The base class of the four basic abstract classes for file operations is externalbyrefobject.

This is an I/O operationThe base class of the object that uses the proxy to exchange messages to communicate across application domain boundaries.

The following is an explanation of this class on msdn:

An application domain is the partition where one or more applications reside in an operating system process. Objects in the same application domain can communicate directly. There are two communication methods for objects in different application domains: one is to transmit object copies across application domain boundaries, and the other is to exchange messages using a proxy.

MarshalbyrefobjectIt is the base class of an object that uses proxy to exchange messages to communicate across application domain boundaries. Not fromMarshalbyrefobjectThe inherited objects are implicitly enclosed by values. When a Remote Application references an object sent by value, a copy of the object is transferred across application domain boundaries.

MarshalbyrefobjectObjects can be directly accessed within the boundaries of the local application domain. First Application access in the remote application domainMarshalbyrefobjectThe proxy is passed to the remote application. The call after the proxy will be sent back to the object residing in the local application domain.

When the type is used across application domain boundaries, the type must be fromMarshalbyrefobjectBecause the object members cannot be used outside the application domains where they are created, the object state cannot be copied.

2. Net I/O.

In this case, I/O is not described in detail, but only some useful functions are introduced.

(1) obtain all rows of a text file

In the file class, there is a method readalllines (string path, encoding) that can retrieve all rows in the text file.

(2) Get the readalltext (string path, encoding) of the text file in the form of a string)

(3) Get the file content in byte array readallbytes (string path)

(4) write operations for the above operations

Writeallbytes(StringPath,Byte[] Bytes );

Writealllines(StringPath,String[] Contents,EncodingEncoding );

Writealltext(StringPath,StringContents,EncodingEncoding );

(5) append text to the file

Appendalltext(StringPath,StringContents,EncodingEncoding );

(6) retrieve all directories in a path

Getdirectories(StringPath,StringSearchpattern,SearchoptionSearchoption );

(7) retrieve all files in a path

      getfiles    (  string   path,   string   searchpattern,   searchoption   searchoption);  

in . net provides many convenient I/O method, if you have time, study file , directory , driveinfo , you will have other gains.

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.