C # file operation FileStream class detailed

The first thing to be clear is that the FileStream class operates on bytes and byte arrays

Introduction to commonly used constructors

The simplest constructor has only two parameters, namely the file name and FileMode enumeration value

FileStream(fliename , FileMode.<Member>)

Another commonly used constructor has three parameters, the third parameter is a member of the FileAccess enumeration, which specifies the role of the stream

FileStream(fliename , FileMode.<Member> ,FileAccess.<Member>)

FileMode enumeration members:

member File exists file does not exist
Append Open the file, the stream points to the end of the file, and can only be used in conjunction with the enum FileAccess.Write Create a new file. Can only be used in conjunction with the enumeration FileAccess.Write
Create Delete the file, and then create a new file Create new file
CreateNew Throw an exception Create new file
Open Open the 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 new file
Truncate Open the file and clear its contents. The stream points to the beginning of the file, retaining the original creation date of the file Throw an exception

FileAccess enumeration members:

member Explanation
Read Open file for read-only
Write Open file for writing only
ReadWrite Open file for reading and writing

1. File location

The FileStream class maintains an internal file pointer, which points to the location in the file for the next read and write operation. In most cases, when you open a file, it points to the beginning of the file, but you can modify this pointer, the way to achieve this function is the Seek () method.

The Seek () method has two parameters: the first parameter specifies the file pointer movement distance (in bytes). The second parameter specifies the starting position to start the calculation, expressed as a value of the SeekOrigin enumeration. The SeekOrigin enumeration contains 3 values: Begin, Current, and End.

E.g:

FileStream file = File.OpenRead("config.txt");

file.Seek(6,SeekOrigin.Begin);//将文件指针移动到文件的第六个字节处,其起始位置就是文件的第一个字节

file.Seek(6,SeekOrigin.Current);//将指针从当前位置移动6个字节

file.Seek(-6,SeekOrigin.End);//将指针移动到文件的倒数第6个字节

2. Read the data

Use the Read () method: read data from the file, and then write the data to a byte array.

Read () has three parameters: the first parameter is the incoming byte array, which is used to store the data in the FileStream object, the second parameter is the position in the byte array to start writing data, usually 0, the third Parameters specify how many bytes to read from the file

FileStream file = new FileStream("home.txt",FileMode.Open);

byte[] byteData = new byte[1024];

file.Seek(10,SeekOrigin.Begin);

file.Read(byteData,0,1024);//从home.txt文件的第10个字节开始读取1024个字节写入byteData字节数组

3. Write data

Use Write () method: read data from the byte array, and then write the data to the file.

Like the Read () method, the Write () method also has three parameters

In the above example, 1024 bytes have been read from the text of home.txt into the byteData byte array, and we will write the data of this array to a new file

FileStream newFile = new FileStream("Temp.txt",FileModel.Create);

newFile.Seek(0,SeekOrigin.Begin);

newFile.Write(byteData,0,byteData.Length);//将byteData字节数组里的数据写入到Temp.txt文件中,从第0个字节位置开始

 

Published 60 original articles · won 84 · 10,000+ views

Guess you like

Origin blog.csdn.net/qq_38992372/article/details/105683930