File copier Learn programming Visual Basic (VB.net)

Lesson:

File Management


Exercise:

File copier


Objetive:

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

mycopy file.txt e:\file2.txt

The program should handle cases where the source file does not exist, and it should warn the user (but not overwrite) if the destination file already 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

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.