您正在尋找這樣的:
var bytes = File.ReadAllBytes(@"yourpathtofile");
或者更類似:
using (var filestream = File.Open(@"C:\apps\test.txt", FileMode.Open))
using (var binaryStream = new BinaryReader(filestream))
{
for (var i = 0; i < arraysize; i++)
{
Console.WriteLine(binaryStream.ReadInt32());
}
}
或者,一個小例子單元測試:
創建整數二進制文件.. 。
[Test]
public void WriteToBinaryFile()
{
var ints = new[] {1, 2, 3, 4, 5, 6, 7};
using (var filestream = File.Create(@"c:\apps\test.bin"))
using (var binarystream = new BinaryWriter(filestream))
{
foreach (var i in ints)
{
binarystream.Write(i);
}
}
}
而從二進制文件
[Test]
public void ReadFromBinaryFile()
{
// Approach one
using (var filestream = File.Open(@"C:\apps\test.bin", FileMode.Open))
using (var binaryStream = new BinaryReader(filestream))
{
var pos = 0;
var length = (int)binaryStream.BaseStream.Length;
while (pos < length)
{
var integerFromFile = binaryStream.ReadInt32();
pos += sizeof(int);
}
}
}
從二進制文件
[Test]
public void ReadFromBinaryFile2()
{
// Approach two
using (var filestream = File.Open(@"C:\apps\test.bin", FileMode.Open))
using (var binaryStream = new BinaryReader(filestream))
{
while (binaryStream.PeekChar() != -1)
{
var integerFromFile = binaryStream.ReadInt32();
}
}
}
來源
2013-02-09 13:36:27
bas
你使用哪個LZW庫? – Magnus 2013-02-09 13:48:43
@LZW,這是我的課http://pastebin.com/KX2veyxe – Denis 2013-02-09 13:52:14