C# - 如何读取和写入二进制文件?
如何从任何文件中读取原始字节数组,并将该字节数组写回到新文件中?
How do I read a raw byte array from any file, and write that byte array back into a new file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
(编辑:请注意,问题发生了变化;最初没有提到
byte[]
;请参阅 修订版 1)好吧,
File.Copy
跃入脑海;但除此之外,这听起来像是一个 Stream 场景:(edit: note that the question changed; it didn't mention
byte[]
initially; see revision 1)Well,
File.Copy
leaps to mind; but otherwise this sounds like aStream
scenario:您了解 TextReader 和 TextWriter 及其后代 StreamReader 和 StreamWriter?我认为这些将解决您的问题,因为它们处理编码,BinaryReader 不知道编码甚至文本,它只关心字节。
如何从文件中读取文本
如何将文本写入文件
这是对文件 IO 和编码的精彩介绍。
Do you know about TextReader and TextWriter, and their descendents StreamReader and StreamWriter? I think these will solve your problem because they handle encodings, BinaryReader does not know about encodings or even text, it is only concerned with bytes.
How to read text from a file
How to write text to a file
This is an excellent intro to file IO and encodings.
添加最新答案时,
您可以选择指定缓冲区大小
,也可以在另一个线程上执行该操作,
这在主线程必须执行其他工作(例如 WPF 和 Windows 应用商店应用程序)时非常有用。
Adding an up to date answer,
you can optionally specify the buffer size
or you could perform the operation on another thread,
which would be useful when the main thread has to do other work, like with WPF and Windows Store apps.