【发布时间】:2015-03-03 08:06:31
【问题描述】:
我有一个有趣的场景,希望其他人偶然发现。我正在尝试将打开的 FileStream 的一部分复制到 c# .Net 3.5 中的单独文件流。使用下面的代码,我读取了 37 个 4096 字节的“块”,但 未能读取最后一个 3812 字节的“部分块”。
string filPath = "c:\aRatherSmallFileThatCouldBeQuiteLarge.log";
string tmpNam = Path.GetTempFileName();
using (FileStream wt = new FileStream(tmpNam, FileMode.Open, FileAccess.Write))
{
using (FileStream rd = new FileStream(filPath, FileMode.Open, FileAccess.Read))
{
long cutPosition = 65217;
int bytesRead;
int chunkSize = 4096;
byte[] buffer = new byte[chunkSize];
while ((bytesRead = rd.Read(buffer, 0, chunkSize)) != 0)
wt.Write(buffer, 0, bytesRead);
}
File.Copy(tmpNam, filPath, true);
}
File.Delete(tmpNam);
没有产生错误;最后的 3812 字节根本没有按预期写入目标文件。
这是一个小例子,但我打算在非常大的文件上使用它......所以将源文件读入内存的解决方案在这里不可行。
【问题讨论】:
标签: .net-3.5 filestream