如何读取/写入文件C#的前128个字节?

时间:2014-03-05 23:22:07

标签: c# .net file byte filestream

我有一堆带有不需要的128字节标头的文件。所以我需要读取/写入前128个字节到文件A,其余的字节到文件B.有人可以帮忙吗? 谢谢。 PS。文件大小从100MB到400GB

        private void SplitUnwantedHeader(string myFile)
    {
        int skipBytes = 128;
        using (FileStream fs = File.Open(myFile, FileMode.Open))
        {
            int bufferSize;
            checked
            {
                bufferSize = (int)(fs.Length - skipBytes);
            }
            byte[] buffer = new byte[bufferSize];

            fs.Position = skipBytes;
            fs.Read(buffer, 0, bufferSize);

            fs.Position = skipBytes;
            fs.Write(buffer, 0, bufferSize);
        }
    }

3 个答案:

答案 0 :(得分:1)

using (FileStream stream = new FileStream())
{
    stream.Write();
}

此流为您要查找的字节的偏移量和计数提供重载。

答案 1 :(得分:0)

private void SplitUnwantedHeader(string myFile)
{
    const int skipBytes = 128;
    using (FileStream fs = File.Open(myFile, FileMode.Open))
    {
        // Write the skipped bytes to file A
        using (FileStream skipBytesFS = File.Open("FileA.txt", FileMode.Create))
        {
            byte[] skipBytesBuffer = new byte[skipBytes];
            fs.Read(skipBytesBuffer, 0, skipBytes);
            skipBytesFS.Write(skipBytesBuffer, 0, skipBytes);
            skipBytesFS.Flush();
        }
        // Write the rest of the bytes to file B
        using (FileStream outputFS = File.Open("FileB.txt", FileMode.Create))
        {
            long length = fs.Length - skipBytes;
            for (long i = 0; i < length; i++)
                outputFS.WriteByte((byte)fs.ReadByte());
            outputFS.Flush();
        }
    }
}

请注意,在将除跳过的字节之外的所有内容写入文件B时,可以将整个输入文件的其余部分读入缓冲区,然后将该缓冲区写入文件B.但是,这很容易导致{{ 1}}如果您的文件真的达到400GB的大小。因此,一次写一个字节。

答案 2 :(得分:0)

我有以下哪个效果很好。此外,还包括一个使用CopyTo()功能的.NET 4.0快捷方式。

.NET 3.5及更低版本的解决方案:

private void SplitUnwantedHeader(string sourceFile, string destinationFile)
    {
        byte[] fByte = new byte[65534];                                                             //Declare 64k byte for read/write buffer
        long headerToSplit = 128;                                                                   //Declare the point where to start reading
        int bytesRead = 0;                                                                          //Declare total bytes read
        try
        {
            using (var fr = new FileStream(sourceFile, FileMode.Open, FileAccess.Read))             //Open source file for reading
            using (var fw = new FileStream(destinationFile, FileMode.Create, FileAccess.Write))     //Create and open destination file for writing
            {
                fr.Position = headerToSplit;                                                        //Set reading position of source file in bytes
                do
                {
                    bytesRead = fr.Read(fByte, 0, fByte.Length);                                    //Read 64k bytes from source file
                    fw.Write(fByte, 0, bytesRead);                                                  //Write 64k bytes to destination file
                } while (bytesRead != 0);                                                           //Loop until there is no more bytes to read
            }               
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);                                   //Catch exception (if any) and display to user
        }
    }

.NET 4.0及更高版本的解决方案:

private void SplitUnwantedHeader(string sourceFile, string destinationFile)
    {
        long headerToSplit = 128;                                                                   //Declare the point where to start reading
        try
        {
            using (var fr = new FileStream(sourceFile, FileMode.Open, FileAccess.Read))             //Open source file for reading
            using (var fw = new FileStream(destinationFile, FileMode.Create, FileAccess.Write))     //Create and open destination file for writing
            {
                fr.Position = headerToSplit;                                                        //Set reading position of source file in bytes
                fr.CopyTo(fw, 65534);        //<-- Alternative for .NET 4.0
            }               
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);                                   //Catch exception (if any) and display to user
        }
    }