帮助读取巨大的二进制文件 [英] help for reading huge binary file

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

问题描述

我遇到了两个有关读取大于20GB的巨大二进制文件的问题.
首先,我的目的是一次将8K字节读入内存并进行处理.
然后我将读取下一个8K字节,直到结束.我如何使用filestream类来实现它?

其次,我尝试通过下面的代码从文件beginnig读取前8 K字节数据到内存,
"tempByteArray"中的值与原始文件不完全相同,某些字节相同,但其他字节不同,为什么?

I met two problems about reading huge binary file which size is more than 20GB.
Firstly,my purpose is to read 8K byte into memory once and deal with them.
then i will read next 8K byte until the end. how can i use the filestream class to achieve it?

Secondly,I try to read first 8 K byte data from file beginnig into memory by code below,
Valuse in "tempByteArray " is not exactly same with origin file,some bytes are same,but others are different,why?

//file is more than 20GB,here is only a test for useing fs.read
FileStream fs = File.OpenRead(filename)
byte[] tempByteArray = new byte[8192];
fs.read(tempByteArray,0,8192);

推荐答案

正如您所说的那样,该文件是二进制文件.听起来像 BinaryReader [ ReadBytes [
As you stated yourself the file is a binary file. Sounds like a job for the BinaryReader[^] class. This type has an instance method calld ReadBytes[^] you can use just like in the sample on that site. This should yield the expected results.

Regards,

—MRB


看看 http://www.java2s.com/Code/CSharp/File-Stream/illustratesuseofBufferedStreams.htm [^ ]
Have a look at the BufferedStream [^]class

Example here...

http://www.java2s.com/Code/CSharp/File-Stream/illustratesuseofBufferedStreams.htm[^]


int read;
const int blocksize = 8192;
byte[] buf = new byte[blocksize];

FileStream fs = new FileStream(filename);

try {
 while(0 < (read = fs.Read(buf, 0, blocksize)){
  // do whatever
 }
} finally { fs.Close(); }


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

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