有效打开大二进制文件 [英] Opening Large Binary file efficiently

查看:86
本文介绍了有效打开大二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿所有,


我需要验证一个大的二进制文件。我只需要阅读最后的

大约100个字节的文件。


这是我目前正在做的但似乎很慢:


private bool ValidFile(string afilename)

{

byte ch;

bool bGraybar = true;

FileStream fs = null;

BinaryReader reader = null;


尝试

{

fs = File.Open(afilename,FileMode.Open);

reader = new BinaryReader(fs);


fs.Seek( -101,SeekOrigin.End);


for(int i = 0; i< = 100; i ++)

{

ch = reader.ReadByte();

if(ch!= 0)

{

bGraybar = false;

休息;

}

}


if(bGraybar)

返回false ;

其他

返回true;

}

终于

{

if(reader!= null)

reader.Close();

if(fs!= null)

{

fs.Close();

fs.Dispose();

}

}

}


~Gina~

解决方案

嘿伙计们,


实际上,由于我的网络,它很慢。 />

无论如何我能得到代码审查吗?


~吉娜〜


< BLOCKQUOTE>" Gina_Marano" < gi ******* @ gmail.com写信息

新闻:11 ********************* @ j44g2000cwa .googlegro ups.com ...


嘿伙计们,


实际上,由于我的网络,它很慢。 br />

无论如何我能获得代码审查吗?



嗯,它慢的另一个原因是你一次读取一个字节。如果

你知道你想要读取100个字节,那么只需一次调用就读取100个字节

到ReadBytes然后直接处理来自字节数组的数据。


当然,在网络上等待也会减慢你的速度,但没有理由让b $ b b使事情变得更糟。


就代码的其余部分而言,我认为你不需要在文件流上调用Dispose

,否则我看不出任何明显的代码

改变。我不清楚为什么你读取最后的101个字节而不是

100,但你确实说100个左右的字节,所以我猜可能没有什么

错了。 :)


Pete


为安全起见,您应该检查文件长度是否> =

你想要读取的字节数。

Peter Duniho写道:


" Gina_Marano" < gi ******* @ gmail.com写信息

新闻:11 ********************* @ j44g2000cwa .googlegro ups.com ...


嘿伙计们,


实际上,由于我的网络,它很慢。 br />

无论如何我能获得代码审查吗?



好​​吧,另一个原因是它很慢,你一次只能读一个字节。如果

你知道你想要读取100个字节,那么只需一次调用就读取100个字节

到ReadBytes然后直接处理来自字节数组的数据。


当然,在网络上等待也会减慢你的速度,但没有理由让b $ b b使事情变得更糟。


就代码的其余部分而言,我认为你不需要在文件流上调用Dispose

,否则我看不出任何明显的代码

改变。我不清楚为什么你读取最后的101个字节而不是

100,但你确实说100个左右的字节,所以我猜可能没有什么

错了。 :)


Pete


Hey all,

I need to validate a large binary file. I just need to read the last
100 or so bytes of the file.

Here is what I am currently doing but it seems slow:

private bool ValidFile(string afilename)
{
byte ch;
bool bGraybar = true;
FileStream fs = null;
BinaryReader reader = null;

try
{
fs = File.Open(afilename, FileMode.Open);
reader = new BinaryReader(fs);

fs.Seek(-101, SeekOrigin.End);

for (int i = 0; i <= 100; i++)
{
ch = reader.ReadByte();
if (ch != 0)
{
bGraybar = false;
break;
}
}

if (bGraybar)
return false;
else
return true;
}
finally
{
if (reader != null)
reader.Close();
if (fs != null)
{
fs.Close();
fs.Dispose();
}
}
}

~Gina~

解决方案

Hey guys,

Actually, it is slow because of my network.

Can I get a code review anyhow?

~Gina~


"Gina_Marano" <gi*******@gmail.comwrote in message
news:11*********************@j44g2000cwa.googlegro ups.com...

Hey guys,

Actually, it is slow because of my network.

Can I get a code review anyhow?

Well, the other reason it''s slow is that you read one byte at a time. If
you know you want to read 100 bytes, then read 100 bytes with a single call
to ReadBytes and then process the data from the byte array directly.

Of course, waiting on the network will slow you down too, but no reason to
make things worse than necessary.

As far as the rest of the code goes, I don''t think you need to call Dispose
on the filestream, but otherwise I don''t see anything obvious that I''d
change. It''s not clear to me why you read the last 101 bytes instead of
100, but you did say "100 or so bytes", so I guess there''s probably nothing
wrong with that. :)

Pete


For safety sake you should probably check that the file length is >= the
number of bytes you want to read.
"Peter Duniho" wrote:

"Gina_Marano" <gi*******@gmail.comwrote in message
news:11*********************@j44g2000cwa.googlegro ups.com...

Hey guys,

Actually, it is slow because of my network.

Can I get a code review anyhow?


Well, the other reason it''s slow is that you read one byte at a time. If
you know you want to read 100 bytes, then read 100 bytes with a single call
to ReadBytes and then process the data from the byte array directly.

Of course, waiting on the network will slow you down too, but no reason to
make things worse than necessary.

As far as the rest of the code goes, I don''t think you need to call Dispose
on the filestream, but otherwise I don''t see anything obvious that I''d
change. It''s not clear to me why you read the last 101 bytes instead of
100, but you did say "100 or so bytes", so I guess there''s probably nothing
wrong with that. :)

Pete


这篇关于有效打开大二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆