在 C# 中将字节数组保存为磁盘文件的最有效方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/128674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the most efficient way to save a byte array as a file on disk in C#?
提问by Kilhoffer
Pretty simple scenario. I have a web service that receives a byte array that is to be saved as a particular file type on disk. What is the most efficient way to do this in C#?
很简单的场景。我有一个 Web 服务,它接收一个字节数组,该数组将作为特定文件类型保存在磁盘上。在 C# 中执行此操作的最有效方法是什么?
采纳答案by Maurice
That would be File.WriteAllBytes()
.
回答by Khoth
System.IO.File.WriteAllBytes(path, data)
should do fine.
System.IO.File.WriteAllBytes(path, data)
应该没问题。
回答by Adam Wright
Not sure what you mean by "efficient" in this context, but I'd use System.IO.File.WriteAllBytes(string path, byte[] bytes)- Certainly efficient in terms of LOC.
不确定在这种情况下您所说的“高效”是什么意思,但我会使用System.IO.File.WriteAllBytes(string path, byte[] bytes)- 在 LOC 方面肯定是有效的。
回答by Guerry
Perhaps the System.IO.BinaryWriter and BinaryReader classes would help.
也许 System.IO.BinaryWriter 和 BinaryReader 类会有所帮助。
http://msdn.microsoft.com/en-us/library/system.io.binarywriter.aspx
http://msdn.microsoft.com/en-us/library/system.io.binarywriter.aspx
"Writes primitive types in binary to a stream and supports writing strings in a specific encoding."
“以二进制形式将原始类型写入流,并支持以特定编码写入字符串。”
http://msdn.microsoft.com/en-us/library/system.io.binaryreader.aspx
http://msdn.microsoft.com/en-us/library/system.io.binaryreader.aspx
"Reads primitive data types as binary values in a specific encoding."
“以特定编码将原始数据类型读取为二进制值。”
回答by ramayac
I had a similar problem dumping a 300 MB Byte array to a disk file...
I used StreamWriter, and it took me a good 30 minutes to dump the file.
Using FilePuttook me arround 3-4 minutes, and when I used BinaryWriter, the file was dumped in 50-60 seconds.
If you use BinaryWriter you will have a better performance.
我在将 300 MB 字节数组转储到磁盘文件时遇到了类似的问题……
我使用了StreamWriter,转储文件花了我 30 分钟。
使用FilePut花了我大约 3-4 分钟,当我使用BinaryWriter 时,文件在 50-60 秒内被转储。
如果您使用 BinaryWriter,您将获得更好的性能。
回答by Kent Boogaart
Actually, the most efficient way would be to stream the data and to write it as you receive it. WCF supports streaming so this may be something you'd want to look into. This is particularly important if you're doing this with large files, since you almost certainly don't want the file contents in memory on both the server and client.
实际上,最有效的方法是流式传输数据并在收到数据时写入。WCF 支持流式传输,因此这可能是您想要研究的内容。如果您要处理大文件,这一点尤其重要,因为您几乎肯定不希望文件内容同时存在于服务器和客户端的内存中。
回答by Chris S
And WriteAllBytes just performs
而 WriteAllBytes 只是执行
using (FileStream stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read))
{
stream.Write(bytes, 0, bytes.Length);
}
BinaryWriter has a misleading name, it's intended for writing primitives as a byte representations instead of writing binary data. All its Write(byte[]) method does is perform Write() on the stream its using, in this case a FileStream.
BinaryWriter 有一个误导性的名称,它旨在将原语编写为字节表示而不是编写二进制数据。它的 Write(byte[]) 方法所做的就是在它使用的流上执行 Write(),在这种情况下是 FileStream。