将文件读取到字节[]的正确方法,反之亦然 [英] Proper way to read a File to a Byte[] and vice versa

查看:138
本文介绍了将文件读取到字节[]的正确方法,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从FileOpenDialog检索了一个FileStream。我有一个Byte [],我打算将b $ b打算存放以供以后使用。将文件存入我的字节[]并将tehn返回到新的FileStream的最有效方法是什么?我一直在看所有的读者和作者,因为信息超载

无法确定正确的方法。


谢谢,


JIM

I have a FileStream retrieved from FileOpenDialog. I have a Byte[] which I
intend to store for later use. What is the most efficient way of getting
the File into my Byte[] and tehn back out to a new FileStream ? I have been
looking at all the Readers and Writers and due to information overload
cannot decide on the proper method.

Thanks,

JIM

推荐答案

James,


这很简单。假设你有一个FileStream并且它位于文件的第一个字节,那么你可以这样做:


//假设pobjStream是文件流。

byte pbytBytes [] =新字节[pobjStream.Length];


//读取字节。

pobjStream。阅读(pbytBytes,0,pobjStream.Length);


这就是真的所有。但是,你必须要小心,对于大型文件来说,这可能会占用大量的内存。如果有可能以较小的块处理您的文件,我建议您这样做。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv * @ spam.guard.caspershouse.com


" james" <无**** @ hypercon.net>在消息中写道

新闻:uV ************* @ TK2MSFTNGP11.phx.gbl ...
James,

It''s quite simple. Assuming that you have a FileStream and it is
positioned at the first byte in the file, you can do this:

// Assume pobjStream is the filestream.
byte pbytBytes[] = new byte[pobjStream.Length];

// Read the bytes.
pobjStream.Read(pbytBytes, 0, pobjStream.Length);

That''s really all there is to it. However, you have to be careful, for
large files, this can take up a huge chunk of memory. If it is possible to
process your file in smaller chunks, I would recommend doing that.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"james" <no****@hypercon.net> wrote in message
news:uV*************@TK2MSFTNGP11.phx.gbl...
我从中检索了一个FileStream FileOpenDialog。我有一个Byte [],其中
我打算存储以供以后使用。将文件放入我的Byte []并将tehn返回到新的FileStream的最有效方法是什么?我有
一直在看所有的读者和作者,由于信息超载
无法决定正确的方法。

谢谢,

JIM
I have a FileStream retrieved from FileOpenDialog. I have a Byte[] which I intend to store for later use. What is the most efficient way of getting
the File into my Byte[] and tehn back out to a new FileStream ? I have been looking at all the Readers and Writers and due to information overload
cannot decide on the proper method.

Thanks,

JIM



Nick,


非常感谢,这应该有用。我想我已经陷入所有Stream

课程以及读者和作家...我错过了简单的方法来做它b
。此外,我一直在寻找像GetBytes这样的方法,或者像MemoryStream上的GetBuffer一样,只需一次性就可以完成所有操作,而无需指定范围。


JIM

Nicholas Paldino [.NET / C#MVP]" < mv*@spam.guard.caspershouse.com>写在

消息新闻:eE ************** @ TK2MSFTNGP09.phx.gbl ...
Nick,

Thanks a lot, that should work. I guess I got caught up in all the Stream
classes and the Readers and Writers... that I missed the simple way to do
it. Also I was looking for a method like GetBytes or like GetBuffer that is
on MemoryStream to do it all in one go without having to specify ranges.

JIM
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:eE**************@TK2MSFTNGP09.phx.gbl...
James,
这很简单。假设你有一个FileStream并且它位于文件的第一个字节,你可以这样做:

//假设pobjStream是文件流。
byte pbytBytes [ ] =新字节[pobjStream.Length];

//读取字节。
pobjStream.Read(pbytBytes,0,pobjStream.Length);

那真的就是这一切。但是,你必须要小心,对于大文件来说,
,这可能会占用大量的内存。如果可以用
来处理你的小文件,我建议你这样做。

希望这会有所帮助。

-
- 尼古拉斯Paldino [.NET / C#MVP]
- mv*@spam.guard.caspershouse.com

詹姆斯 <无**** @ hypercon.net>在消息中写道
新闻:uV ************* @ TK2MSFTNGP11.phx.gbl ...
James,

It''s quite simple. Assuming that you have a FileStream and it is
positioned at the first byte in the file, you can do this:

// Assume pobjStream is the filestream.
byte pbytBytes[] = new byte[pobjStream.Length];

// Read the bytes.
pobjStream.Read(pbytBytes, 0, pobjStream.Length);

That''s really all there is to it. However, you have to be careful, for large files, this can take up a huge chunk of memory. If it is possible to process your file in smaller chunks, I would recommend doing that.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"james" <no****@hypercon.net> wrote in message
news:uV*************@TK2MSFTNGP11.phx.gbl...
我有一个从FileOpenDialog检索到的FileStream。我有一个Byte []
I have a FileStream retrieved from FileOpenDialog. I have a Byte[] which
打算存储供以后使用。什么是最有效的
intend to store for later use. What is the most efficient way of



将文件放入我的Byte []并将tehn返回到新的FileStream?我有


getting the File into my Byte[] and tehn back out to a new FileStream ? I have


已经

查看所有的读者和作者并且由于信息过载而无法确定正确的方法。

谢谢,

JIM
looking at all the Readers and Writers and due to information overload
cannot decide on the proper method.

Thanks,

JIM




Nicholas Paldino [.NET / C#MVP ]< mv*@spam.guard.caspershouse.com>写道:
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
这很简单。假设你有一个FileStream并且它位于文件的第一个字节,你可以这样做:

//假设pobjStream是文件流。
byte pbytBytes [ ] =新字节[pobjStream.Length];

//读取字节。
pobjStream.Read(pbytBytes,0,pobjStream.Length);

那真的就是这一切。但是,你必须要小心,对于大文件来说,这可能会占用大量的内存。如果有可能以较小的块处理您的文件,我建议您这样做。
It''s quite simple. Assuming that you have a FileStream and it is
positioned at the first byte in the file, you can do this:

// Assume pobjStream is the filestream.
byte pbytBytes[] = new byte[pobjStream.Length];

// Read the bytes.
pobjStream.Read(pbytBytes, 0, pobjStream.Length);

That''s really all there is to it. However, you have to be careful, for
large files, this can take up a huge chunk of memory. If it is possible to
process your file in smaller chunks, I would recommend doing that.




这并不是全部。除非我错过了什么,否则

FileStream.Read(和其他流一样)并不能保证让你回报所有

你要求的数据一次性。 (即使FileStream保证

它,我也不建议依赖它 - 这很容易进入

坏习惯,并且当突然你正在从一个

NetworkStream而不是FileStream等读取时没注意到。)


参见 http://www.pobox.com/~skeet/csharp/readbinary.html


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复该群组,请不要也寄给我



That''s not quite all there is to it. Unless I''ve missed something,
FileStream.Read (like other streams) isn''t guaranteed to return you all
the data you asked for in one lump. (Even if FileStream does guarantee
it, I wouldn''t suggest relying on it - it''s far too easy to get into
bad habits that way, and not notice when suddenly you''re reading from a
NetworkStream instead of a FileStream, etc.)

See http://www.pobox.com/~skeet/csharp/readbinary.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


这篇关于将文件读取到字节[]的正确方法,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆