怎样读入一个二进制文件,在头部写上几个字符?
怎样读入一个二进制文件,在头部写上几个字符?
------解决方案--------------------笨狼,好久不见你散分
------解决方案--------------------笨狼在潜水吧
------解决方案--------------------一个byte[]文件内容
一个byte[]要写的内容
Copy到一个新Byte[]就可以了
------解决方案--------------------//怎样读入一个二进制文件,在头部写上几个字符
不知所云~
------解决方案--------------------FileStream.Read 读,.Write写
------解决方案--------------------定义一个byte数组作缓冲区,循环读写
------解决方案--------------------先把文件内容读到内存中,然后再写新加的内容,然后写旧的内容.
BinaryReader
BinaryWriter
------解决方案--------------------定义一个byte数组,先写入你需要写的字符串,然后再在后面添加byte
------解决方案--------------------// 创建测试数据
string path = Server.MapPath( "~/App_Data/ForBinary.data ");
Stream stream = File.Open(path, FileMode.Open);
BinaryWriter writer = new BinaryWriter(stream);
for (int i = 10; i < 100; i++) {
writer.Write(i);
}
writer.Close();
stream.Close();
// 读取数据
stream = new FileStream(path, FileMode.Open);
BinaryReader reader = new BinaryReader(stream);
byte[] oldBuffer = reader.ReadBytes(1024);
//Response.Write(oldBuffer.Length);
reader.Close();
stream.Close();
// 插入头部数据
int insertSize = 10;
byte[] newBuffer = new byte[oldBuffer.Length + insertSize];
for (int i = 0; i < insertSize; i++) {
newBuffer[i] = (byte)i;
}
Array.Copy(oldBuffer, 0, newBuffer, insertSize, oldBuffer.Length);
//Response.Write(newBuffer.Length);
// 写入数据
path = Server.MapPath( "~/App_Data/ForBinary2.data ");
stream = File.Open(path, FileMode.OpenOrCreate, FileAccess.Write);
writer = new BinaryWriter(stream);
writer.Write(newBuffer, 0, newBuffer.Length);
writer.Close();
stream.Close();