如何将磁盘上的文件读入内存流? [英] How can I read a file on disk into a memory stream?

查看:103
本文介绍了如何将磁盘上的文件读入内存流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我在磁盘上有一个名为TEMP.ZIP的文件,我想以某种方式将这个带到内存流中,这样我最终可以这样做:


row [" DOCUMENT"] = dataStream.ToArray()


但是,我不确定获取它的正确方法成为一个记忆

流。


任何帮助表示赞赏。


tia,

机会。

Hello,
I have a file on disk called TEMP.ZIP and I would like to somehow get
this into a memory stream so I can eventually do this:

row["DOCUMENT"] = dataStream.ToArray()

However, I am not sure of the correct way to get it into a memory
stream.

Any help appreciated.

tia,
chance.

推荐答案

机会,


你不需要记忆stream将文件加载到字节数组中。您需要做的所有

打开文件流,然后将内容读入

数组。由于这是一个文件流,因此您需要提前知道长度。你b $ b可以做到这一点:


//字节。

byte [] bytes = null;

//文件流。

使用(FileStream fs = new FileStream(...))

{

//分配bytes。

bytes = new byte [fs.Length];


//剩余的字节数和要读取的位置。

int bytesToRead = bytes.Length;

int readFrom = 0;


//读取的字节数。

int bytesRead = 0;


//读取有字节需要读取。

while(bytesToRead 0)

{

//读取数据。

bytesRead = fs.Read(bytes,readFrom,bytesToRead);


//通过读取的字节数减少剩余字节。

bytesToRead = bytesToRead - bytesRead;


//增加下一个读取操作。

readFrom = readFrom + bytesRead;

}

}


//可以在这里使用你的数组。


我想你会发现这个效率更高。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


机会 < ch **** @ crwmail.com写信息

news:11 ********************** @ n59g2000hsh.googlegr oups.com ...
chance,

You don''t need a memory stream to load the file into a byte array. All
you have to do is open the filestream, and then read the contents into an
array. Since this is a file stream, you know the length in advance. You
can do this:

// The bytes.
byte[] bytes = null;
// The filestream.
using (FileStream fs = new FileStream(...))
{
// Allocate the bytes.
bytes = new byte[fs.Length];

// The number of bytes remaining, and the location to read from.
int bytesToRead = bytes.Length;
int readFrom = 0;

// The bytes read.
int bytesRead = 0;

// Read while there are bytes to be read.
while (bytesToRead 0)
{
// Read the data.
bytesRead = fs.Read(bytes, readFrom, bytesToRead);

// Decrement the remaining bytes by the number of bytes read.
bytesToRead = bytesToRead - bytesRead;

// Increment the next read operation.
readFrom = readFrom + bytesRead;
}
}

// Can use your array here.

I think you will find this much more efficient.

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

"chance" <ch****@crwmail.comwrote in message
news:11**********************@n59g2000hsh.googlegr oups.com...

您好,

我在磁盘上有一个名为TEMP.ZIP的文件,我想以某种方式得到

这是一个内存流,所以我最终可以这样做:


row [" DOCUMENT"] = dataStream.ToArray()


但是,我不确定将它放入内存的正确方法

stream。


任何帮助表示赞赏。


tia,

机会。
Hello,
I have a file on disk called TEMP.ZIP and I would like to somehow get
this into a memory stream so I can eventually do this:

row["DOCUMENT"] = dataStream.ToArray()

However, I am not sure of the correct way to get it into a memory
stream.

Any help appreciated.

tia,
chance.



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

消息新闻:uL ************** @ TK2MSFTNGP04.phx.gbl ...
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:uL**************@TK2MSFTNGP04.phx.gbl...

机会,


您不需要内存流来将文件加载到字节数组中。您需要做的所有

打开文件流,然后将内容读入

数组。由于这是一个文件流,因此您需要提前知道长度。你

可以做到这一点:$ b​​ $ b
chance,

You don''t need a memory stream to load the file into a byte array. All
you have to do is open the filestream, and then read the contents into an
array. Since this is a file stream, you know the length in advance. You
can do this:



我也有类似的问题,但我有一系列结构(有2

int16,3 int32)

是否有一种方法可以直接从文件中读取结构到struct struct而不是
而不是一个字节[]?


目前我正在使用一个单独的字节数组来读取文件,

然后使用marshal类从字节做一个不安全的副本到

结构数组。

这也可能对OP有用。


工作正常,但有直接方法吗?所需的内存量非常大。

我可以得到一个不安全的ptr到struct array ok,但是文件需要一个字节[]

是否存在较低级别的不安全读取?


我正在查看序列化但我没有找到很多关于它的信息

呢,

我花了很长时间看(我真的需要修复我的帮助所以它的

在线)


它只是为了缓存csv文件需要更长时间才能加载。


Colin = ^。^ =

I also have similar problem, but I have an array of structures (with 2
int16,3 int32)
is there a way to read this directly from file into the struct array rather
than a byte[]?

at the moment im using a seperate byte array to read from the file,
then using the marshal class to do an unsafe copy from the byte to the
struct array.
this might be useful to the OP as well.

works fine but is there a direct way ? the amount of memory needed is quite
large.
I can get an unsafe ptr to the struct array ok, but the file needs a byte[]
is there a lower level unsafe read ?

I was looking at serialize but ive not managed to find much out about it
yet,
ive spent quite some time looking (I realy need to fix my help so its
online)

Its just to cache the csv file wich takes much longer to load.

Colin =^.^=


colin,

在这种情况下,使用不安全的代码可能更容易(如果您的解决方案

允许)。由于你的结构很简单(结构本身

不包含任何指针或对其他任何内容的引用),你可以直接阅读

内容。基本上,你打开文件,然后对ReadFileEx(通过P / Invoke层)进行

调用,将指针传递给你的

结构作为缓冲区。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" colin" < co ********* @ ntworld.NOSPAM.com写信息

新闻:d9 **************** @ newsfe5- win.ntli.net ...
colin,

In this case, it might be easier to use unsafe code (if your solution
allows for that). Since your structures are simple (the structure itself
doesn''t contain any pointers or references to anything else), you can read
the contents directly. Basically, you would open the file, and then make a
call to ReadFileEx (through the P/Invoke layer), passing a pointer to your
structure as the buffer.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"colin" <co*********@ntworld.NOSPAM.comwrote in message
news:d9****************@newsfe5-win.ntli.net...

" Nicholas Paldino [.NET / C#MVP]" < mv*@spam.guard.caspershouse.com写了
消息新闻中的
:uL ************** @ TK2MSFTNGP04.phx.gbl ...
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:uL**************@TK2MSFTNGP04.phx.gbl...

>机会,

你不需要内存流来将文件加载到字节数组中。
所有你要做的就是打开文件流,然后将内容读入数组。由于这是一个文件流,因此您知道
advance的长度。你可以这样做:
>chance,

You don''t need a memory stream to load the file into a byte array.
All you have to do is open the filestream, and then read the contents
into an array. Since this is a file stream, you know the length in
advance. You can do this:



我也有类似的问题,但我有一个结构数组(有2

int16,3 int32)

有没有办法直接从文件中读取这个结构数组

而不是一个字节[]?


at当我使用一个单独的字节数组从文件中读取时,

然后使用marshal类从字节到

struct数组执行不安全的复制。 />
这也可能对OP有用。


工作正常,但有直接方法吗?所需的内存量是

相当大。

我可以得到一个不安全的ptr到struct array ok,但是文件需要一个

byte []

是否存在较低级别的不安全读取?


我正在查看序列化但是我没有找到很多关于它的内容

尚未,

我花了很长时间看(我真的需要修复我的帮助所以它的

在线)


它只是缓存csv文件,需要更长时间才能加载。


Colin = ^。^ =


I also have similar problem, but I have an array of structures (with 2
int16,3 int32)
is there a way to read this directly from file into the struct array
rather than a byte[]?

at the moment im using a seperate byte array to read from the file,
then using the marshal class to do an unsafe copy from the byte to the
struct array.
this might be useful to the OP as well.

works fine but is there a direct way ? the amount of memory needed is
quite large.
I can get an unsafe ptr to the struct array ok, but the file needs a
byte[]
is there a lower level unsafe read ?

I was looking at serialize but ive not managed to find much out about it
yet,
ive spent quite some time looking (I realy need to fix my help so its
online)

Its just to cache the csv file wich takes much longer to load.

Colin =^.^=



这篇关于如何将磁盘上的文件读入内存流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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