VB - How do I read and write a binary file?

谁都会走 提交于 2020-01-31 05:29:06

问题


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()

回答1:


Dim data() as Byte = File.ReadAllBytes(path1)
File.WriteAllBytes(path2, data)



回答2:


System.IO.File.ReadAllBytes("myfile.txt")



回答3:


Try this:-

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


来源:https://stackoverflow.com/questions/1450568/vb-how-do-i-read-and-write-a-binary-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!