B4J Question Read/Copy specified bytes from a binary file

WAZUMBi

Well-Known Member
Licensed User
I want to remove the first and last 512 bytes from a binary file. How can I do that with B4j?
 

Erel

Administrator
Staff member
Licensed User
Assuming that the file is not too large:
B4X:
Dim temp(512) As Byte
Dim Data(File.Size(...) - 1024) As Byte
Dim In As InputStream = File.OpenInput(...)
ReadBytesFully(In, temp, 512)
ReadBytesFully(In, Data, Data.Length)
In.Close
Dim out As OutputStream = File.OpenOutput(...)
out.WriteBytes(Data, 0, Data.Length)
out.Close


Private Sub ReadBytesFully(In As InputStream, Data() As Byte, Len As Int) As Byte()
   Dim count = 0, read As Int
   Do While count < Len AND read > -1
     read = In.ReadBytes(Data, count, Len - count)
     count = count + read
   Loop
   Return Data
End Sub
 
Upvote 0

howard bassen

Member
Licensed User
I have a question:

Where is the data file located? What is the name of the file, and how do you specify the path to the file?

Thanks

-------------------------------------------------



Assuming that the file is not too large:
B4X:
Dim temp(512) As Byte
Dim Data(File.Size(...) - 1024) As Byte
Dim In As InputStream = File.OpenInput(...)
ReadBytesFully(In, temp, 512)
ReadBytesFully(In, Data, Data.Length)
In.Close
Dim out As OutputStream = File.OpenOutput(...)
out.WriteBytes(Data, 0, Data.Length)
out.Close


Private Sub ReadBytesFully(In As InputStream, Data() As Byte, Len As Int) As Byte()
   Dim count = 0, read As Int
   Do While count < Len AND read > -1
     read = In.ReadBytes(Data, count, Len - count)
     count = count + read
   Loop
   Return Data
End Sub
 
Upvote 0
Top