Link to home
Start Free TrialLog in
Avatar of thaburner
thaburnerFlag for United States of America

asked on

Split File into Multiple Byte Arrays

I was able to read a file into one byte array and write it back in another file without any issue.  However I'm trying to figure out how to read one file and split it into multiple arrays.  Then write back all the arrays into one file.  I'm not sure exactly what is going on but it seems I keep getting one extra byte in my arrays than I should have.

Working Simple Array
Imports System.IO

        Dim myFileStream As FileStream
        Dim intByte As Integer
        Dim lngLoop As Long = 0
        Dim bteRead() As Byte

        Try
            myFileStream = File.OpenRead("C:\Users\Guest\Desktop\Adobe.pdf")
            ReDim bteRead(myFileStream.Length)

            Do While Not intByte = -1
                intByte = myFileStream.ReadByte()
                If intByte <> -1 Then bteRead(lngLoop) = CByte(intByte)
                lngLoop += 1
            Loop

            myFileStream.Close()

            Dim oFileStream As System.IO.FileStream
            oFileStream = New System.IO.FileStream("C:\Users\Guest\Desktop\Adobe Test.pdf", System.IO.FileMode.Create)
            oFileStream.Write(bteRead, 0, bteRead.Length)
            oFileStream.Close()
        Catch ex As IOException
            Console.WriteLine(ex.Message)
        End Try

Open in new window


Code for attempting to split the file into byte arrays.
Imports System.IO

        Dim myFileStream As FileStream
        Dim intByte As Integer
        Dim lngLoop As Long = 0
        Dim BinBlock1 As Byte()
        Dim BinBlock2 As Byte()

        BinBlock1 = Nothing
        BinBlock2 = Nothing

        Try
            myFileStream = File.OpenRead("C:\Users\Guest\Desktop\Adobe.pdf")
            ReDim BinBlock1(50000) 'Total Size = 73,478
            ReDim BinBlock2(23478)

            Do While Not intByte = -1
                intByte = myFileStream.ReadByte()
                If intByte <> -1 Then
                    If (lngLoop >= 0 And lngLoop <= 50000) Then
                        BinBlock1(lngLoop) = CByte(intByte)
                    End If

                    If (lngLoop >= 50001 And lngLoop < 99000) Then
                        If (lngLoop = 50001) Then lngLoop = 0
                        BinBlock2(lngLoop) = CByte(intByte)
                    End If
                End If
                lngLoop += 1
            Loop

            myFileStream.Close()

            Dim oFileStream As System.IO.FileStream
            oFileStream = New System.IO.FileStream("C:\Users\Guest\Desktop\Adobe Test.pdf", System.IO.FileMode.Create)
            oFileStream.Write(BinBlock1, 0, BinBlock1.Length)
            oFileStream.Write(BinBlock2, 0, BinBlock2.Length)
            oFileStream.Close()
        Catch ex As IOException
            Console.WriteLine(ex.Message)
        End Try

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of thaburner

ASKER

This will work perfectly!!! Thanks Idle