将文件转换为字节 [英] convert file to byte
本文介绍了将文件转换为字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何将文件转换为字节
并更改此字节
并使用新文件保存更改
thnx寻求帮助
how i can convert file to bytes
and change this byte
and save a change with new file
thnx for any help
推荐答案
- 使用
File.ReadAllBytes
读取文件中的字节. - 根据需要直接修改返回的字节数组.
- 使用
File.WriteAllBytes
保存修改后的字节. /li>
- Use
File.ReadAllBytes
to read the bytes in the file. - Directly modify the returned byte array as you please.
- Use
File.WriteAllBytes
to save the modified bytes.
----------
下面的示例代码(由OP取代):
----------
Sample code below (requsted by OP):
byte[] bytes = File.ReadAllBytes(@"d:\abc.pdf");
bytes[10] = 34;
bytes[11] = 89;
File.WriteAllBytes(@"d:\abc.pdf", bytes);
Nishant Sivakumar提供了一个很好的答案,但我想指出一个适用于较大文件的替代方法:
使用 MemoryMappedFile [MemoryMappedViewAccessor [
Nishant Sivakumar has provided a good answer, but I''d like to point to an alternative suitable for larger files:
Use MemoryMappedFile[^] with MemoryMappedViewAccessor[^] to read and write data.
Perhaps like this (copied from the documentation):
using (var accessor = mmf.CreateViewAccessor(offset, length))
{
int colorSize = Marshal.SizeOf(typeof(MyColor));
MyColor color;
// Make changes to the view.
for (long i = 0; i < length; i += colorSize)
{
accessor.Read(i, out color);
color.Brighten(10);
accessor.Write(i, ref color);
}
}
.Net 4引入了处理内存映射文件的功能,因此很容易被忽略.
问候
Espen Harlinn
This ability to work with memory mapped files was introduced with .Net 4 - so it''s easy to overlook.
Regards
Espen Harlinn
这篇关于将文件转换为字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文