vb.net VB - 如何读取和写入二进制文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1450568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:26:08  来源:igfitidea点击:

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

vb.netbytearrayfilestreambinaryfiles

提问by TheFlash

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

回答by Tommy Carlier

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

回答by Darin Dimitrov

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

回答by AnthonyWJones

Try this:-

尝试这个:-

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