我正在尝试编写然后读取一个大的随机文件来计算磁盘速度。我尝试了几种算法,但在尝试写1GB文件时仍然遇到输出或内存异常。以下是我尝试的一些
方法1
isWrapperFor()
方法2
byte[] data = new byte[8192];
Random rng = new Random();
using (FileStream stream = File.OpenWrite(filePath))
{
for (int i = 0; i < fileSizeMb * 128; i++)
{
rng.NextBytes(data);
stream.Write(data, 0, data.Length);
}
}
阅读
const int blockSize = 1024 * 8;
const int blocksPerMb = (1024 * 1024) / blockSize;
int count = fileSizeMb * blocksPerMb;
byte[] data = new byte[blockSize];
Random rng = new Random();
//using (FileStream stream = File.OpenWrite(filePath))
using (StreamWriter sw1 = new StreamWriter(filePath))
{
// There
for (int i = 0; i < count; i++)
{
rng.NextBytes(data);
sw1.BaseStream.Write(data, 0, data.Length);
//stream.Write(data, 0, data.Length);
}
}
答案 0 :(得分:2)
方法1
byte[] data = new byte[8192];
Random rng = new Random();
using (FileStream stream = File.OpenWrite(filePath))
{
for (int i = 0; i < fileSizeMb * 128; i++)
{
rng.NextBytes(data);
stream.Write(data, 0, data.Length);
stream.Flush(); // BEETLE JUICE
}
}
方法2
const int blockSize = 1024 * 8;
const int blocksPerMb = (1024 * 1024) / blockSize;
int count = fileSizeMb * blocksPerMb;
byte[] data = new byte[blockSize];
Random rng = new Random();
using (StreamWriter sw1 = new StreamWriter(filePath))
{
// There
for (int i = 0; i < count; i++)
{
rng.NextBytes(data);
sw1.BaseStream.Write(data, 0, data.Length);
sw1.baseStream.Flush(); // BEETLE JUICE
}
}
阅读
不要将整个文件读入内存,一次只读取4096个字节。 http://www.csharp-examples.net/filestream-read-file/
的示例代码