File copier Learn programming Visual Basic (VB.net)

Lesson:

File Management


Exercise:

File copier 44


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


Code:

Imports System
Imports System.IO
Public Class FileCopier
    Public Shared Sub Main()
        Const BUFFER_SIZE As Integer = 512 * 1024
        Dim data As Byte() = New Byte(524287) {}
        Dim inFile As FileStream = File.OpenRead("1.exe")
        Dim outFile As FileStream = File.Create("1-copy.exe")
        Dim amountRead As Integer

        Do
            amountRead = inFile.Read(data, 0, BUFFER_SIZE)
            outFile.Write(data, 0, amountRead)
        Loop While amountRead = BUFFER_SIZE

        inFile.Close()
        outFile.Close()
    End Sub
End Class