本人是初学者,今天利用FileStream类写了一个读写文件的程序,其中FileStream.Write()方法需要一个Byte类型的数组,但是数组的长度应该如何确定?如果文件对应的二进制长度小于数组,读取后再写入自动补0,文件大小也变化了。。。。,应该如何解决这个问题?
SaveFileDialog saveFile = new SaveFileDialog ();
if (saveFile.ShowDialog() == DialogResult.OK)
{
string readPath=tbFilePath.Text;
FileStream readStream=new FileStream(readPath,FileMode.Open);
string savePath = saveFile.FileName;
FileStream saveStream = new FileStream(savePath, FileMode.CreateNew);
byte[] buffer= new byte[1024*1024];
int i=0;
do
{
i= readStream.Read(buffer,0,buffer.Length);
saveStream.Write(buffer,0,buffer.Length);
}while(buffer.Length==i);
MessageBox.Show("写入成功!!!");
readStream.Dispose();
saveStream.Dispose();
}