8.25 - File copier

Objetive

Create a program to copy a source file into a destination file. You must use FileStream and a block size of 512 Kb. An example of use might be:

mycopy file.txt e:\file2.txt

It must behave correctly if the source file does not exist and it must warn (but not overwrite it) if the destination file exists


Solution

using System;
using System.IO;

public class FileCopier
{

    public static void Main()
    {
        const int BUFFER_SIZE = 512 * 1024;
        
        byte[] data = new byte[BUFFER_SIZE];
        
        FileStream inFile = File.OpenRead("1.exe");
        FileStream outFile = File.Create("1-copy.exe");

        int amountRead;
        
        do
        {
            amountRead = inFile.Read(data,0,BUFFER_SIZE);
            outFile.Write(data,0,amountRead);
        }
        while (amountRead == BUFFER_SIZE);
        
        inFile.Close();
        outFile.Close();
    }
}


Popular Posts