Replication of large files in C #

Source: Internet
Author: User

Recently learned two classes, FileStream, what does this class do?

Let's take a look at the official explanations given by MSDN.

FileStream: Exposes a file-based Streamthat supports both synchronous read and write operations as well as asynchronous read and write operations.

Small understanding: Mainly in the form of flow to manipulate the file.


What is a stream?

In daily life, there are traffic, water, and so on, and it is the existence of the flow, it is not traffic congestion, flooding, but in a form of flow slowly to dredge (I do not know whether to understand). For example, we in the operation of the file copy, the direct copy, will be the large capacity of the file directly into memory, resulting in a very slow replication, and take the stream, will be the file in binary form, slowly copy, thereby improving the speed.


Replication of large files

Here's a copy of a large file, to familiarize yourself with the operation of this class.


The entire process is as shown, the binary file is read into the buffer through a stream, and the file is copied at the end of the write.


Demo Show

//read in large file operation static void Main (string[] args) {//file stream reads large file using (FileStream fs = new FileStream ( @ "D:\ software 3\java\jdk-8u11-windows-i586.1406279697.exe", FileMode.OpenOrCreate)) {//write operation using (File Stream FS2 = new FileStream (@ "C:\Users\ZHOU\Desktop\1.exe", FileMode.Create)) {//read-only 1M at a time,                    Define buffer byte[] buffer = new byte[1024 * 1024*5];//used to determine byte int n =-1;                                                Do {//return value, number of bytes actually read, loop read file to buffer n = fs. Read (buffer, 0, buffer.                        Length); Circular write Operation FS2. Write (buffer, 0, buffer.                                     Length);                             } while (n > 0); }                        }                    }

Summarize

There's a lot more to do with the flow, and it's interesting to look at the next MSDN.







Replication of large files in C #

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.