C# 如何读取/写入文件C的前128个字节?
我有一堆带有不需要的128字节头的文件。因此,我需要将前128个字节读/写到文件A,将其余字节读/写到文件B。有人能帮忙吗? 谢谢 文件大小从100MB到400GB不等C# 如何读取/写入文件C的前128个字节?,c#,.net,file,byte,filestream,C#,.net,File,Byte,Filestream,我有一堆带有不需要的128字节头的文件。因此,我需要将前128个字节读/写到文件A,将其余字节读/写到文件B。有人能帮忙吗? 谢谢 文件大小从100MB到400GB不等 private void SplitUnwantedHeader(string myFile) { int skipBytes = 128; using (FileStream fs = File.Open(myFile, FileMode.Open)) {
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);
}
}
此流为您正在查找的字节的偏移量和计数提供重载。private void splituntendeheader(字符串myFile)
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();
}
}
}
{
常量int skipBytes=128;
使用(FileStream fs=File.Open(myFile,FileMode.Open))
{
//将跳过的字节写入文件A
使用(FileStream skipbytes=File.Open(“FileA.txt”,FileMode.Create))
{
字节[]skipBytes缓冲=新字节[skipBytes];
fs.Read(skipBytes缓冲区,0,skipBytes);
写入(skipBytes缓冲区,0,skipBytes);
SkipBytes.Flush();
}
//将其余字节写入文件B
使用(FileStream outputFS=File.Open(“FileB.txt”,FileMode.Create))
{
长长度=fs.length-skipBytes;
用于(长i=0;i
请注意,在将跳过的字节以外的所有内容写入文件B时,可以将输入文件的其余部分全部读入缓冲区,然后将该缓冲区写入文件B。但是,如果文件的大小真的达到400GB,这很容易导致
OutOfMemoryException
。因此,一次写入一个字节。我有以下方法,效果很好。此外,还包括一个.NET 4.0快捷方式,它使用CopyTo()
功能
针对.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
}
}
private void splitUntentedHeader(字符串源文件,字符串目标文件)
{
long HeaderSplit=128;//声明开始读取的点
尝试
{
使用(var fr=new FileStream(sourceFile,FileMode.Open,FileAccess.Read))//打开源代码文件进行读取
使用(var fw=new FileStream(destinationFile,FileMode.Create,FileAccess.Write))//创建并打开目标文件进行写入
{
fr.Position=headerToSplit;//以字节为单位设置源文件的读取位置
fr.CopyTo(fw,65534);//read方法包含您需要的选项。
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
}
}