c# - 在 WHILE 循环中处理 C# 文件流输入导致执行时间错误

标签 c# .net filestream

我有一个 C# 控制台应用程序,我正在尝试创建它来处理给定目录中的所有文件并将输出写入另一个给定目录。我想一次处理 X 个字节的输入文件。

namespace FileConverter
{
    class Program
    {
        static void Main(string[] args)
        {
            string srcFolder = args[0];  
            string destFolder = args[1];   
            string[] srcFiles = Directory.GetFiles(srcFolder);
            for (int s = 0; s < srcFiles.Length; s++)
            {
                byte[] fileBuffer;
                int numBytesRead = 0;
                int readBuffer = 10000;
                FileStream srcStream = new FileStream(srcFiles[s], FileMode.Open, FileAccess.Read);
                int fileLength = (int)srcStream.Length;

                string destFile = destFolder + "\\" + Path.GetFileName(srcFiles[s]) + "-processed";
                FileStream destStream = new FileStream(destFile, FileMode.OpenOrCreate, FileAccess.Write);

                //Read and process the source file by some chunk of bytes at a time
                while (numBytesRead < fileLength)
                {
                    fileBuffer = new byte[readBuffer];

                    //Read some bytes into the fileBuffer
                    //TODO: This doesn't work on subsequent blocks
                    int n = srcStream.Read(fileBuffer, numBytesRead, readBuffer);

                    //If we didn't read anything, there's no more to process
                    if (n == 0)
                        break;

                    //Process the fileBuffer
                    for (int i = 0; i < fileBuffer.Length; i++)
                    {
                        //Process each byte in the array here
                    }
                    //Write data
                    destStream.Write(fileBuffer, numBytesRead, readBuffer);
                    numBytesRead += readBuffer;
                }
                srcStream.Close();
                destStream.Close();
            }
        }
    }
}

我在执行时遇到错误:

//Read some bytes into the fileBuffer
//TODO: This doesn't work on subsequent blocks
int n = srcStream.Read(fileBuffer, numBytesRead, readBuffer);

我不想将整个文件加载到内存中,因为它的大小可能有好几千兆字节。我真的希望能够读取一定数量的字节,处理它们,将它们写入文件,然后读入下一个 X 字节并重复。

它通过循环的一次迭代,然后在第二次循环中死亡。我得到的错误是:

“偏移量和长度超出数组范围或计数大于从索引到源集合末尾的元素数。”

我正在使用的示例文件大约为 32k。

谁能告诉我我做错了什么?

最佳答案

读取的第二个参数不是文件的偏移量——它是开始写入数据的缓冲区的偏移量。所以只传递 0。

此外,不要假设缓冲区每次都被填满:您应该只处理缓冲区中的“n”个字节。缓冲区应该在迭代之间重用。

如果您需要读取恰好多个字节:

static void ReadOrThrow(Stream source, byte[] buffer, int count) {
     int read, offset = 0;
     while(count > 0 && (read = source.Read(buffer, offset, count)) > 0) {
        offset += read;
        count -= read;
    }
    if(count != 0) throw new EndOfStreamException();
}

请注意,Write 的工作方式类似,因此您需要传递 0 作为偏移量和 n 作为计数。

关于c# - 在 WHILE 循环中处理 C# 文件流输入导致执行时间错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9571002/

相关文章:

c# - 使用 Linq 读取 XML 文档

c# - Entity Framework (EF) OutOfMemoryException

node.js - 只读取修改后的数据

c# - 如何以可移植的方式在 C# 中播放压缩的声音文件?

c# - 为什么它总是一个空值?

c - 从c中的文件流中获取数据

c++ - 如果文件中的一行包含特定字符,则在 C++ 中查找

c# - 无法成功启动或连接到子 MSBuild.exe 进程。验证 MSBuild.exe

c# - 在 .net Core 中调用 SOAP 服务

c# - 为什么回发后我的数据丢失了