VB - 如何读写二进制文件?
-
11-09-2019 - |
题
如何阅读从任意文件原始字节数组...
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()
解决方案
Dim data() as Byte = File.ReadAllBytes(path1)
File.WriteAllBytes(path2, data)
其他提示
System.IO.File.ReadAllBytes("myfile.txt")
尝试这种情况: -
Dim bytes() as Byte
bytes = File.ReadAllBytes(fileName)
'' # Do stuff to the array
File.WriteAllBytes(otherFileName, bytes)
不隶属于 StackOverflow