Filestream.Read返回值是什么意思?如何分块读取数据并进行处理? [英] What does Filestream.Read return value mean? How to read data in chunks and process it?

查看:418
本文介绍了Filestream.Read返回值是什么意思?如何分块读取数据并进行处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C#还是很陌生,所以请多多包涵.我正在读取(使用FileStream)数据(固定大小)为小数组,处理数据,然后再次读取,依此类推直到文件末尾.

I'm quite new to C# so please bear with me. I'm reading (using FileStream) data (fixed size) to small array, process the data and then read again and so on to the end of file.

我考虑过使用这样的东西:

I thought about using something like this:

            byte[] data = new byte[30];
            int numBytesToRead = (int)fStream.Length;
            int offset = 0;

            //reading
            while (numBytesToRead > 0)
            {
                fStream.Read(data, offset, 30);
                offset += 30;
                numBytesToRead -= 30;

                //do something with the data
            }

但是我检查了文档及其示例,他们说上述read方法的返回值为:

But I checked documentation and their examples and they stated that return value of the above read method is:

类型:System.Int32 读入缓冲区的字节总数.如果该字节数当前不可用,则该数目可能小于所请求的字节数;如果已到达流的末尾,则该数目可能为零."

"Type: System.Int32 The total number of bytes read into the buffer. This might be less than the number of bytes requested if that number of bytes are not currently available, or zero if the end of the stream is reached."

这意味着它们当前不可用,这在读取少量数据时会真的发生吗?或者只是大量读取而已?如果只是大的话,大约要大多少,因为在其他一些地方,我也会以更大的块来阅读.如果任何时候都可能发生这种情况,我应该如何更改代码,以使代码仍能高效执行?

What does it mean that they are not currently available, can this really happen when reading small amounts of data or is this just for large amounts? If only for large, how large approximately, because I'll be reading also in bigger chunks in some other places. If this can happen anytime how should I change my code so that the code will still execute efficiently?

感谢您的时间和答复.

推荐答案

read方法返回返回的字节数,该字节数可能小于请求的字节数.通常,当您读取文件时,将获得您要求的所有字节(除非到达文件末尾),但是,您不能指望它总是这样.

The read method returns the number of bytes returned, which may be less than the number of bytes requested. Normally when you read a file, you will get all the bytes that you ask for (unless you reach the end of the file), however, you can't count on it always being that way.

系统可能会在立即可用的数据和需要检索时间的数据之间产生差异,以便立即返回当前可用的数据,开始在后台读取更多数据,并希望您能够在另一个调用中请求其余数据. AFAIK目前不这样做,但这是一个合理的未来方案.

It's possible that the system will make a difference between data that is immediately available and data that needs time to be retrieved, so that it will return the data currently available right away, start reading more data in the background and expect you to request the rest of the data in another call. AFAIK it doesn't do this currently, but it's a reasonable future scenario.

您应该获得Read方法的结果,并使用该结果确定获得了多少数据.您不应将其读取到offset位置的缓冲区中,否则您将无法读取大于缓冲区的文件.另外,您可以声明一个数组以容纳整个流,然后将数据读入offset的位置.

You should get the result of the Read method and use that to determine how much data you got. You shouldn't read it into the buffer at the location of offset, then you can't read a file that is larger than the buffer. Alternatively, you can declare an array to hold the entire stream, then you would read the data into the location of offset.

您还应该处理Read方法返回零的情况,这意味着没有更多数据可读取.通常,直到到达文件末尾,这种情况才会发生,但是如果这样做,它将使您的代码陷入永恒的循环.

You should also handle the situation where the Read method returns zero, which means that there is no more data to read. This normally doesn't happen until you reach the end of the file, but if it would it would throw your code into an eternal loop.

byte[] data = new byte[30];
int numBytesToRead = (int)fStream.Length;
int offset = 0;

//reading
while (numBytesToRead > 0) {
  int len = fStream.Read(data, 0, data.Length);
  offset += len;
  numBytesToRead -= len;
  if (len == 0 && numBytesToRead > 0) {
    // error: unexpected end of file
  }
  //do something with the data (len bytes)
}

这篇关于Filestream.Read返回值是什么意思?如何分块读取数据并进行处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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