C # FileStream file read/write

Source: Internet
Author: User

The FileStream object indicates the stream that points to the file on the disk or network path. This class provides methods to read and write bytes in files. However, StreamReader or StreamWriter is often used to perform these functions. This is because the FileStream class operates on Byte and byte arrays, while the Stream class operates on character data. Character data is easy to use, but some operations, such as random File Access (access to data at a point in the middle of the file), must be executed by the FileStream object, which will be described later.

There are also several ways to create a FileStream object. Constructor has many different overload versions. The simplest constructor only has two parameters, namely, the file name and FileMode enumeration value.

 

FileStream aFile = new FileStream(filename, FileMode.Member);

 

The FileMode enumeration contains several members that specify how to open or create a file. We will introduce these enumeration members later. Another common constructor is as follows:

 

FileStream aFile = new FileStream(filename, FileMode.Member, FileAccess. Member);

 

The third parameter is a member of the FileAccess enumeration, which specifies the role of the stream. The FileAccess enumerated members are shown in table 22-6.

Table 22-6

Members

Description

Read

Open a file for read-only

Write

Open a file to write only

ReadWrite

Open a file for reading and writing

An exception is thrown if the object is not specified by the FileAccess enumeration member. This attribute changes the user's access permissions to files based on the user's authentication level.

In versions where the FileStream constructor does not use the FileAccess enumeration parameter, use the default value FileAccess. ReadWrite.

The FileMode enumerated members are shown in table 22-7. What happens when each value is used depends on whether the specified file name represents an existing file. Note that the items in this table indicate the position of the stream pointing to the file when the stream is created. The topic will be discussed in the next section. Unless otherwise specified, the stream points to the beginning of the file.

Table 22-7
 

Members

File stored in

File does not exist

Append

Open the file and the stream points to the end of the file. It can only be used together with the enumerated FileAccess. Write

Create a new file. It can only be used with enumeration FileAccess. Write

Create

Delete the file and create a new file.

Create a new file

CreateNew

Throw an exception

Create a new file

Open

Open an existing file. The stream points to the beginning of the file.

Throw an exception

OpenOrCreate

Open the file. The stream points to the beginning of the file.

Create a new file

Truncate

Open an existing file and clear its content. The stream points to the beginning of the file and retains the initial creation date of the file.

Throw an exception

 

 

 

 

 

 

 

 

 

 

 

 

Both the File and FileInfo classes provide OpenRead () and OpenWrite () methods, making it easier to create FileStream objects. The former opens the file for read-only access, and the latter can only write files. These provide shortcuts, so you do not have to provide all the preceding information in the form of FileStream constructor parameters. For example, the following code line opens the data.txt file that is used only to read logs:

 

FileStream aFile = File.OpenRead("Data.txt");

 

Note that the following code executes the same function:

 

FileInfo aFileInfo = new FileInfo("Data.txt");FileStream aFile = aFile.OpenRead();

 

1. File Location

The FileStream class maintains an internal file pointer pointing to the next read/write operation in the file. In most cases, when a file is opened, it points to the starting position of the file, but this pointer can be modified. This allows applications to read and write files anywhere, access files randomly, or directly jump to a specific location of the file. This saves a lot of time when processing large files, because you can immediately locate the correct location.

The method to implement this function is the Seek () method, which 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 of SeekOrigin enumeration. The Seek Origin enumeration contains three values: Begin, Current, and End.

For example, the following code line moves the file pointer to the first byte of the file. Its starting position is the first byte of the file:

 

aFile.Seek(8,SeekOrigin.Begin);

 

The following code row moves the pointer two bytes forward from the current position. If the following code is executed after the preceding code line, the file Pointer Points to the 10th bytes of the file:

 

aFile.Seek(2,SeekOrigin.Current);

 

Note that when reading and writing files, the file pointer also changes. When reading

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.