views:

454

answers:

3

How do I read a raw byte array from any file...

 Dim bytes() as Byte

..and then write that byte array back into a new file?

I need it as a byte array to do some processing in between.


I'm currently using:

To read

 Dim fInfo As New FileInfo(dataPath)
 Dim numBytes As Long = fInfo.Length
 Dim fsAs New FileStream(dataPath, FileMode.Open, FileAccess.Read)
 Dim br As New BinaryReader(fs)
 Dim bytes As Byte() = br.ReadBytes(CInt(numBytes))
 br.Close()
 fs.Close()

To write

Dim fs As System.IO.FileStream
fs = New System.IO.FileStream(outpath, System.IO.FileMode.Create)
fs.Write(bytes, 0, bytes.Length)
fs.Close()
+2  A: 
System.IO.File.ReadAllBytes("myfile.txt")
Darin Dimitrov
+3  A: 
Dim data() as Byte = File.ReadAllBytes(path1)
File.WriteAllBytes(path2, data)
Tommy Carlier
+1  A: 

Try this:-

Dim bytes() as Byte
bytes = File.ReadAllBytes(fileName)
'' # Do stuff to the array
File.WriteAllBytes(otherFileName, bytes)
AnthonyWJones