【问题标题】:Read and write of binary file in c#c#中二进制文件的读写
【发布时间】:2020-09-03 19:16:17
【问题描述】:

我已按照以下代码使用 C# 读取二进制文件。然后我尝试将这个二进制数据写入另一个二进制文件。但是我发现当我在Winmerge中打开这两个文件时,两个二进制文件是有区别的。即读取文件和写入文件。 如果我只是读取文件并重写,您能否建议为什么会有不同?

       string fileNameWithPath_ = "1.pwpmi";
       string newfileNameWithPath_ = "2.pwpmi";

        System.IO.FileStream fileStream = new System.IO.FileStream(fileNameWithPath_, System.IO.FileMode.Open,
            System.IO.FileAccess.Read);
        System.IO.BinaryReader binReader = new System.IO.BinaryReader(fileStream, Encoding.ASCII);


        char[] chararr = new char[fileStream.Length];

        chararr = binReader.ReadChars((int)fileStream.Length);
        byte[] buffer = binReader.ReadBytes((int)fileStream.Length);

        byte[] bytes = new byte[fileStream.Length];
        fileStream.Read(bytes,0, (int)fileStream.Length);

        byte[] fileBytes = System.IO.File.ReadAllBytes(fileNameWithPath_);
        string stringbyte1 = Encoding.ASCII.GetString(fileBytes);

        binReader.Close();
        fileStream.Close();
        System.IO.BinaryWriter binWriter =
        new System.IO.BinaryWriter(System.IO.File.Open(newfileNameWithPath_, System.IO.FileMode.Create));
        binWriter.Flush();
        binWriter.Write(stringbyte1);
        binWriter.Close();

【问题讨论】:

  • 这段代码中唯一可以保存的可能是System.IO.FileStream fileStream = new System.IO.FileStream(...)。你能描述一下你真正想要做什么吗?您只是想创建文件的副本吗?还是更改第一个部分的某些部分,将结果保存在第二个中?还有什么?
  • 你为什么要让这件事变得比File.Copy(srcPath, dstPath)更难,或者最坏的情况(如果你真的想拦截或转换你复制的数据):File.WriteAllBytes(dstPath, File.ReadAllBytes(srcPath))

标签: c# binary binaryfiles binaryreader binarywriter


【解决方案1】:

您似乎已经尝试了几种不同的方法,并且实际上已经非常接近有效的方法。问题可能在于您将二进制数据作为一种数据类型读取并将其作为另一种数据类型写回输出的方式。尝试坚持bytes

    string fileNameWithPath_ = "1.pwpmi";
    string newfileNameWithPath_ = "2.pwpmi";

    System.IO.FileStream fileStream = new System.IO.FileStream(fileNameWithPath_, System.IO.FileMode.Open,
        System.IO.FileAccess.Read);
    System.IO.BinaryReader binReader = new System.IO.BinaryReader(fileStream, Encoding.ASCII);
    byte[] fileBytes = binReader.ReadBytes((int)fileStream.Length);
    //byte[] fileBytes = System.IO.File.ReadAllBytes(fileNameWithPath_); // this also works

    binReader.Close();
    fileStream.Close();
    System.IO.BinaryWriter binWriter =
    new System.IO.BinaryWriter(System.IO.File.Open(newfileNameWithPath_, System.IO.FileMode.Create));
    binWriter.Flush();
    binWriter.Write(fileBytes); // just feed it the contents verbatim
    binWriter.Close();

当我通过 WinMerge 运行时,上面的代码不会对传入的字节流进行任何更改并生成相同的文件

正如 cmets 所建议的,您最好完全复制文件:

    string fileNameWithPath_ = "1.pwpmi";
    string newfileNameWithPath_ = "2.pwpmi";
    File.Copy(fileNameWithPath_, newfileNameWithPath_, overwrite: true);

【讨论】:

  • 我不想复制文件我想在另一个文件中写入二进制数据 您的代码运行良好。谢谢
【解决方案2】:

.NET 框架提供了一种复制文件的内置方法:

File.Copy(fileNameWithPath_, newfileNameWithPath_)

(这里,FileSystem.IO.File。)

或者:

using (FileStream inStream = new FileStream(fileNameWithPath_, FileMode.Open, FileAccess.Read))
using (FileStream outStream = new FileStream(newfileNameWithPath_, FileMode.Create, FileAccess.Write))
{
    inStream.CopyTo(outStream);
}

【讨论】:

    猜你喜欢
    • 2019-04-20
    • 2019-09-18
    • 2013-07-10
    • 2020-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-22
    • 2012-01-26
    相关资源
    最近更新 更多