C#将文件拆分为两个字节数组

C#将文件拆分为两个字节数组,c#,arrays,file,byte,C#,Arrays,File,Byte,因为字节数组的最大值是2GB,所以假设我有一个更大的文件,需要将其转换为字节数组。既然我不能保存整个文件,我应该如何将其转换为两个 我试过: long length = new System.IO.FileInfo(@"c:\a.mp4").Length; int chunkSize = Convert.ToInt32(length / 2); byte[] part2; FileStream fileStream = new FileStream(filepath, FileMode.O

因为字节数组的最大值是2GB,所以假设我有一个更大的文件,需要将其转换为字节数组。既然我不能保存整个文件,我应该如何将其转换为两个

我试过:

long length = new System.IO.FileInfo(@"c:\a.mp4").Length;
int chunkSize = Convert.ToInt32(length / 2); 


byte[] part2;
FileStream fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read);
try
{
    part2 = new byte[chunkSize];            // create buffer
    fileStream.Read(part2, 0, chunkSize);
}
finally
{
    fileStream.Close();
}

byte[] part3;
fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read);
try
{
    part3 = new byte[chunkSize];            // create buffer
    fileStream.Read(part3, 5, (int)(length - (long)chunkSize));
}
finally
{
    fileStream.Close();
}
但它不起作用


有什么想法吗?

您可以使用
StreamReader
阅读


您可以使用
StreamReader
读入


如果使用的文件非常大,则应使用,将物理文件映射到内存空间:

using (var mmf = MemoryMappedFile.CreateFromFile(@"c:\path\to\big.file"))
{
    using (var accessor = mmf.CreateViewAccessor())
    {
        byte myValue = accessor.ReadByte(someOffset);
        accessor.Write((byte)someValue);
    }
}
另见:


您还可以使用
MemoryMappedViewAccessor

中的不同方法读取/写入文件块。如果您正在处理超大文件,则应使用,它将物理文件映射到内存空间:

using (var mmf = MemoryMappedFile.CreateFromFile(@"c:\path\to\big.file"))
{
    using (var accessor = mmf.CreateViewAccessor())
    {
        byte myValue = accessor.ReadByte(someOffset);
        accessor.Write((byte)someValue);
    }
}
另见:

您还可以在
MemoryMappedViewAccessor
中使用不同的方法读取/写入文件块这是我的解决方案:

byte[] part1;
byte[] part2;
bool odd = false;
int chunkSize = Convert.ToInt32(length/2);


if (length % 2 == 0)
{
    part1 = new byte[chunkSize];
    part2 = new byte[chunkSize];
}
else
{
    part1 = new byte[chunkSize];
    part2 = new byte[chunkSize + 1];
    odd = true;
}

FileStream fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read);
using (fileStream)
{
    fileStream.Seek(0, SeekOrigin.Begin);
    int bytesRead = fileStream.Read(part1, 0, chunkSize);
    if (odd)
    {
        bytesRead = fileStream.Read(part2, 0, chunkSize + 1);
    }
    else
    {
        bytesRead = fileStream.Read(part2, 0, chunkSize);
    }
}
这就是我的解决方案:

byte[] part1;
byte[] part2;
bool odd = false;
int chunkSize = Convert.ToInt32(length/2);


if (length % 2 == 0)
{
    part1 = new byte[chunkSize];
    part2 = new byte[chunkSize];
}
else
{
    part1 = new byte[chunkSize];
    part2 = new byte[chunkSize + 1];
    odd = true;
}

FileStream fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read);
using (fileStream)
{
    fileStream.Seek(0, SeekOrigin.Begin);
    int bytesRead = fileStream.Read(part1, 0, chunkSize);
    if (odd)
    {
        bytesRead = fileStream.Read(part2, 0, chunkSize + 1);
    }
    else
    {
        bytesRead = fileStream.Read(part2, 0, chunkSize);
    }
}

读取您需要将整个文件保存在内存中吗?当您需要对一个大文件执行某些操作时,您应该找到一种方法,如果您可以帮助的话,它不会尝试将整个文件读取到内存中。@Tigran我想不出其他选项。@AaYy那么,在内存中保存这些字节数组之后,您打算如何处理它们?可能有一种方法不需要在内存中保存整个文件。像那样把它拼凑起来,无论如何都会破坏任何合理的理由。这基本上是一个读取过程。你需要将整个文件保存在内存中吗?当你需要处理一个大文件时,你应该找到一种方法,如果你能帮助的话,它不会尝试将整个文件读取到内存中。@Tigran我想不出其他选项。@AaYy那么,在内存中保存这些字节数组之后,你打算如何处理它们?可能有一种方法不需要在内存中保存整个文件。像那样把它拼凑起来,无论如何都会破坏任何合理的理由。这基本上是一个数组,那么保存数据的数组到底是什么呢?是缓冲区吗?
buffer
将数据保存在
start
start+max-1
之间。文档,那么保存数据的数组到底是什么?是缓冲区吗?
buffer
将数据保存在
start
start+max-1
之间。为什么偏移量始终为0的文档?FortyTwo有一个更通用、更好的解决方案。在使用OOP语言编程时,应该记住可重用性和抽象性。为什么偏移量总是0?FortyTwo有一个更通用、更好的解决方案。在使用OOP语言编程时,应牢记可重用性和抽象性。