VB - 如何读写二进制文件?

时间:2009-09-20 08:07:27

标签: vb.net bytearray filestream binaryfiles

如何从任何文件中读取原始字节数组...

 Dim bytes() as Byte

..然后将该字节数组写回新文件?

我需要它作为字节数组来进行一些处理。


我目前正在使用:

阅读

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

Dim fs As System.IO.FileStream
fs = New System.IO.FileStream(outpath, System.IO.FileMode.Create)
fs.Write(bytes, 0, bytes.Length)
fs.Close()

3 个答案:

答案 0 :(得分:15)

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

答案 1 :(得分:5)

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

答案 2 :(得分:3)

试试这个: -

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