File copier
Last updated:
8/23/2020
⁃
Difficulty:
Intermediate
Create a C# program that makes copies of both text and binary files. You can use FileStream
to assign a buffer size of 512 Kb.
Input
Output
Solution
- using System.IO;
- public class FileCopier
- {
- public static void Main(string[] args)
- {
- const int BUFFER_SIZE = 512 * 1024;
- byte[] data = new byte[BUFFER_SIZE];
-
- string inputFileName = "app.exe";
- string outputFileName = "app-copy.exe";
-
- using (FileStream inputFile = File.OpenRead(inputFileName))
- {
- using (FileStream outputFile = File.Create(outputFileName))
- {
- int amountRead;
- do
- {
- amountRead = inputFile.Read(data, 0, BUFFER_SIZE);
- outputFile.Write(data, 0, amountRead);
- }
- while (amountRead == BUFFER_SIZE);
- }
- }
- }
- }