Invert binary file V2 Learn programming with Visual Basic (VB.net) exercises

Lesson:

File Management


Exercise:

Invert binary file V2 48


Objetive:

Create a program to "invert" a file, using a "FileStream": create a file with the same name ending in ".inv" and containing the same bytes as the original file but in reverse order (the first byte will be the last, the second will be the penultimate, and so on, until the last byte of the original file, which should appear in the first position of the resulting file).

You must deliver only the ".cs" file, which should contain a comment with your name


Code:

Imports System
Imports System.IO
Class InverterFileStream
    Private Shared Sub Main(ByVal args As String())
        Dim fileName As String
        Console.Write("Enter the name of file to convert: ")
        fileName = Console.ReadLine()
        Dim myFileReader As FileStream = File.OpenRead(fileName)
        Dim size As Long = myFileReader.Length
        Dim data As Byte() = New Byte(size - 1) {}
        myFileReader.Read(data, 0, CInt(size))
        myFileReader.Close()
        Dim myFileWriter As FileStream = File.Create(fileName & ".inv")

        For i As Long = size - 1 To 0
            myFileWriter.WriteByte(data(i))
        Next

        myFileWriter.Close()
    End Sub
End Class