thaburner
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
Code for attempting to split the file into byte arrays.
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER