C# uses cache to read and write large files in chunks for your reference. The specific content is as follows
In daily life, you may encounter reading large files. No matter what format it is, if you read large files according to the format of the stored files, you will see the relevant file header content in the Buffer. Take the .txt file access as an example.
using ;
First create the demo file, the file size here doesn't matter, it's just a demonstration
private void button2_Click(object sender, EventArgs e) { using (FileStream fsWrite = new FileStream(@"D:\", )) { string temp = ""; for (int i = 0; i < 10000;i++ ) { temp += ()+"/t"; } byte [] m = . (temp); (m, 0, ); } }
Read the created file
private void Readtxt() { using (FileStream fsRead = new FileStream(@"d:\,")) { //The length of the remaining file content long leftLength = ; //buffersize int buffersize = 1024; //Create cache array byte[] buffer = new byte[buffersize]; int rNum = 0; int FileStart = 0; while(leftLength > 0) { //Set the read location of the file stream = FileStart ; if (leftLength < buffersize) { rNum = (buffer, 0, Convert.ToInt32(leftLength)); } else { rNum = (buffer, 0, maxLength); } if (rNum == 0) { break; } fileStart += rNum; leftLength -= rNum; //Byte conversion string msg = .(buffer);// byte[] myByte = .(msg);// //Write to file using (FileStream fsWrite = new FileStream(@"d:\, ))//Additional addition after completion { (myByte, 0, ); } } (); } }
Write to the file later, and also involves data splicing and processing
Personally, if the data needs to be spliced in a certain format, it can be processed by changing the location of each reading.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.