File System Data

Source: Internet
Author: User

Stream

 

Stream is an abstract representation of serialized devices.

Two types of streams:
Output stream: When writing data to some external targets, the output stream is used.
Input stream: used to read data to the memory or variable that the program can access.

Class used for Input and Output

System. Io Contains classes used to read and write data in files

File and directory

The file and directory catalog classes provide many static methods for processing files and directories.
These methods can move files, query, and modify attributes to create filestream objects.

Common static methods for file:
Method description
Copy () copies the question file from the source location to the target location
Create () create a file in the specified path
Delete () delete an object
Open () returns the filestream object in the specified path
Move () to move the specified file to a new location. You can customize the file name.

 

Common static methods of directory:
Method description
Createdirectory () create a directory with a specified path
Delete () delete a specified directory and all files in it
Getdirectories () returns the string object array representing the directory name in the current directory
Getfiles () returns the string object array of the file name in the current directory
Getfilesystementries () returns the array of string objects of files and directory names in the current directory.
Move () to move the specified directory to a new location. You can customize the folder name.

Fileinfo class

The fileinfo class is used to instantiate objects.
The fileinfo object indicates the file on the disk or network.
You can create a fileinfo object by providing the file path.
For example:
Fileinfo afile = new fileinfo (@ "C: \ log.txt ");

Note:
Prefix @ indicates that the string should be interpreted one by one. If there is no @ prefix, use "\" instead of "\".

Many methods of the fileinfo class are similar to those of the file class.
For example:
Fileinfo afile = new fileinfo ("data.txt ");

If (afile. exists)
{
....
}

Equivalent to the file class
If (file. exists ("data.txt "))
{
....
}

Applicability:
If you only call a single method, the method on the static file class will be faster.
If the application executes multiple operations on the file, you do not need to search for the file every time you use the fileinfo class.

 

The fileinfo class also provides attributes related to underlying files.
Attribute description
Attributes uses the fileattributes enumeration to obtain or set the attributes of the current file or directory.
Creationtime: Get the current file creation date and time
Extension extract File Extension
Exists
Fullname: full path of the retrieved File
Lastaccesstime: Get or set the date and time of the last access to the current file
Lastwritetime: Get or set the date and time of the last write to the current file
Get File Name

Fileinfo special attributes
Attribute description
Property retrieves a directoryinfo object, indicating the directory containing the current file
Directoryname: Path of the returned file directory
Shortcuts to the read-only attribute of isreadonly files
Length to get the file capacity

The fileinfo object itself does not represent a stream. To read and write files, you must create a stream object.

Directoryinfo class

The directoryinfo class is an instantiated object that represents a single directory on the computer.
Many method calls can be copied between directory and directoryinfo.
For a single call, use a fast static Directory class
If multiple calls are performed, the instantiated directoryinfo object is used.

Directoryinfo special attributes
Attribute description
Parent indicates the directory containing the current directory
Root indicates the root directory containing the current directory

Filestream object

The filestream object indicates the stream that points to the file on the disk or network path.

You can create a filestream object using multiple methods.
For example:
Filestream afile = new filestream (filename, filemode. member );
The parameters are file names and filemode enumeration classes.
Filemode enumeration has several rules on how to open or create a file

Another common constructor:
Filestream afile = new filestream (filename, filemode. Member, fileaccess. member );
The third parameter is a member of the fileaccess enumeration and specifies the role of the stream.

Fileaccess enumeration members:
Member description
Read
Write to open the file, used to write only
Readwrite opens the file for reading and writing (default)

Filemode enumeration members:
The member file does not exist.
Append: open the file and point the stream to the end of the file to create a new file.
Create to delete the file, and then create a new file to create a new file
Createnew throws an exception to create a new file.
Open open file. An exception is thrown when the stream points to the beginning of the file.
Openorcreate open the file, and the stream points to the beginning of the file to create a new file
Truncate open the file, clear the content, point to the beginning, and throw an exception

Both the file and fileinfo classes provide easier openread () and openwrite () methods to create filestream.
The former opens the file for read-only access, and the latter only allows file writing.

For example:
Filestream afile = file. openread ("data.txt ");

Similarly:
Fileinfo afileinfo = new fileinfo ("data.txt ");
Filestream afile = afileinfo. openread ();

File Location

The filestream class maintains an internal file pointer pointing to the next read/write operation in the file.

You can use the seek () method to locate the specified position.
It has two parameters:
The first parameter specifies the moving distance of the file pointer in bytes.
The second parameter specifies the start position for calculation, which is expressed by a value in seekorigin enumeration.
Seekorigin enumeration includes: Begin, current, and end

For example:
Afile. Seek (8, seekorigin. Begin );
Move the file pointer to the first byte of the file, starting from the first byte of the file.

Afile. Seek (2, seekorigin. Current );
Move the pointer from the current position to 2 bytes

When reading and writing files, the file pointer also changes
For example, after reading 10 bytes, the file pointer will point to 11th bytes.

You can specify the position for negative lookup.
For example:
Afile. Seek (-5, seekorigin. End );
Searches for the last 5th bytes in the file.

Read data

The filestream. Read () method can access data from files pointed to by the filestream object.
This method reads the byte block from the stream and writes the data into the given buffer.

It contains three parameters:
The first parameter is the transmitted byte array to accept the data in the filestream object.
The second parameter is the location where data is written in the byte array.
The last parameter is the maximum number of bytes read.

The returned value is the total number of bytes read into the buffer.

Write Data

Use the filestream. Write () method to write data to a file
This method writes bytes into the stream using the data read from the buffer zone.

It contains three parameters:
The first parameter contains the buffer for the data to be written into the stream.
The second parameter is the location where data replication starts in the byte array.
The last parameter is the maximum number of bytes to write to the current stream.

Streamwriter object

The streamwriter class allows writing characters and strings to files.
Create a streamwriter object:
Filestream afile = new filestream ("log.txt", filemode. createnew );
Streamwriter Sw = new streamwriter (afile );
Alternatively, you can directly create a streamwriter object from the file:
Streamwriter Sw = new streamwriter ("log.txt", true );

The second parameter is a bool value, specifying whether to add it to the end of the file or create a new file:
If it is flase, create a new file, or intercept an existing file and open it.
If the value is true, open the file and retain the original data. If the file cannot be found, create a new file.

Streamreader object

The streamreader class is used to read data from files and can be used for any stream.
Create a streamreader object:
Filestream afile = new filestream ("log.txt", filemode. Open );
Streamreader sr = new streamreader (afile );
Alternatively, you can directly create a streamreader object from the file:
Streamreader sr = new streamreader ("log.txt ");

Read data

The read () and Readline () Methods of streamreader are used to read data.
For example:
Streamreader sr = new streamreader (afile );
Int nchar;
Nchar = Sr. Read ();
While (nchar! =-1)
{
Console. Write (convert. tochar (nchar ));
Nchar = Sr. Read ();
}
Sr. Close ();

You can use the readtoend () method to read a small file.
For example:
Streamreader sr = new streamreader (afile );
Strline = Sr. readtoend ();
Console. writeline (strline );
Sr. Close ();

Files separated by delimiters

Some files are stored as separator Separated Files
The split () method of the string class can be used to convert a string to an Array Based on the provided delimiter.

Read and Write compressed files

The system. Io. Compression namespace has two compression classes: deflatestream and gzipstream.
You only need to specify whether the stream is used for compression or decompression at the top to perform corresponding operations.

 

Serialized object

The system. runtime. serialization and system. runtime. serialization. formatters Namespaces provide the basic architecture of serialized objects.
There are two available implementation methods:
System. runtime. serialization. formatters. Binary
Contains the binaryformatter class, which serializes the object and binary data.

System. runtime. serialization. formatters. Soap
Contains the soapformatter class, which serializes XML data in the object and SOAP format.

Both classes implement the iformatter interface. The method of this interface is as follows:
Method description
Void serialize (Stream stream, object source) serializes source into Stream
Object deserialize (Stream stream) serializes the data in stream and returns the object

For example:
Use binaryformatter for serialization:
Iformatter serializer = new binaryformatter ();
Serializer. serialize (mystream, myobject );

Iformatter serializer = new binaryformatter ();
Myobjecttype mynewobject = serializer. deserialize (mystream) as myobjecttype;

 

Monitoring file structure

Use the filesystemwatcher class to monitor the file structure
The filesystemwatcher class provides several applications to capture events, such as modifying files or directories.

Attributes that must be set before the filesystemwatcher object is Enabled:
Attribute description
Path: Set the file location or directory to be monitored
The policyfilter policyfilters enumerated value specifies the content to be monitored.
Filter the filter of the monitoring file, for example, *. txt

Then, you must write a handler for four events changed, created, deleted, and renamed.

Set the enableraisingevents attribute to true to start monitoring.

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.