110,499
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var bytes = generateTestData();
saveBytes("data", bytes);
var bytes2 = reloadBytes("data");
//AssertEqual(bytes, bytes2);
}
static object reloadBytes(string fileName)
{
var fh = File.OpenRead(fileName);
var result = new List<byte[]>();
while (true)
{
var length = fh.ReadByte();
if (length == -1)
{
break;
}
length = length * 256 + fh.ReadByte();
var bs = new byte[length];
result.Add(bs);
fh.Read(bs, 0, length);
}
fh.Close();
return result.ToArray();
}
static void saveBytes(string fileName, byte[][] bytes)
{
var fh = File.OpenWrite(fileName);
foreach (var item in bytes)
{
fh.WriteByte((byte)(item.Length / 256));
fh.WriteByte((byte)(item.Length % 256));
fh.Write(item, 0, item.Length);
}
fh.Close();
}
static byte[][] generateTestData()
{
var r = new Random();
var result = new List<byte[]>();
for (var i = 0; i < 500; i++)
{
var bytes = new byte[800];
r.NextBytes(bytes);
result.Add(bytes);
}
return result.ToArray();
}
}
}