C# - 如何读取和写入二进制文件?

发布于 2024-08-05 01:47:06 字数 39 浏览 4 评论 0原文

如何从任何文件中读取原始字节数组,并将该字节数组写回到新文件中?

How do I read a raw byte array from any file, and write that byte array back into a new file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

森林很绿却致人迷途 2024-08-12 01:47:07

(编辑:请注意,问题发生了变化;最初没有提到 byte[] ;请参阅 修订版 1

好吧,File.Copy 跃入脑海;但除此之外,这听起来像是一个 Stream 场景:

    using (Stream source = File.OpenRead(inPath))
    using (Stream dest = File.Create(outPath)) {
        byte[] buffer = new byte[2048]; // pick size
        int bytesRead;
        while((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0) {
            dest.Write(buffer, 0, bytesRead);
        }
    }

(edit: note that the question changed; it didn't mention byte[] initially; see revision 1)

Well, File.Copy leaps to mind; but otherwise this sounds like a Stream scenario:

    using (Stream source = File.OpenRead(inPath))
    using (Stream dest = File.Create(outPath)) {
        byte[] buffer = new byte[2048]; // pick size
        int bytesRead;
        while((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0) {
            dest.Write(buffer, 0, bytesRead);
        }
    }
墨落画卷 2024-08-12 01:47:07
byte[] data = File.ReadAllBytes(path1);
File.WriteAllBytes(path2, data);
byte[] data = File.ReadAllBytes(path1);
File.WriteAllBytes(path2, data);
哆兒滾 2024-08-12 01:47:07

您了解 TextReaderTextWriter 及其后代 StreamReaderStreamWriter?我认为这些将解决您的问题,因为它们处理编码,BinaryReader 不知道编码甚至文本,它只关心字节。

如何从文件中读取文本

如何将文本写入文件

这是对文件 IO 和编码的精彩介绍。

Do you know about TextReader and TextWriter, and their descendents StreamReader and StreamWriter? I think these will solve your problem because they handle encodings, BinaryReader does not know about encodings or even text, it is only concerned with bytes.

How to read text from a file

How to write text to a file

This is an excellent intro to file IO and encodings.

も星光 2024-08-12 01:47:07

添加最新答案时,

using (var source = File.OpenRead(inPath))
{
    using (var dest = File.Create(outPath))
    {
        source.CopyTo(dest);
    }
}

您可以选择指定缓冲区大小

using (var source = File.OpenRead(inPath))
{
    using (var dest = File.Create(outPath))
    {
        source.CopyTo(dest, 2048); // or something bigger.
    }
}

,也可以在另一个线程上执行该操作,

using (var source = File.OpenRead(inPath))
{
    using (var dest = File.Create(outPath))
    {
        await source.CopyToAsync(dest);
    }
}

这在主线程必须执行其他工作(例如 WPF 和 Windows 应用商店应用程序)时非常有用。

Adding an up to date answer,

using (var source = File.OpenRead(inPath))
{
    using (var dest = File.Create(outPath))
    {
        source.CopyTo(dest);
    }
}

you can optionally specify the buffer size

using (var source = File.OpenRead(inPath))
{
    using (var dest = File.Create(outPath))
    {
        source.CopyTo(dest, 2048); // or something bigger.
    }
}

or you could perform the operation on another thread,

using (var source = File.OpenRead(inPath))
{
    using (var dest = File.Create(outPath))
    {
        await source.CopyToAsync(dest);
    }
}

which would be useful when the main thread has to do other work, like with WPF and Windows Store apps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文