.NET Micro Framework,在内存有限的设备上读取文件

发布于 2024-10-08 02:46:49 字数 248 浏览 8 评论 0原文

在 ChipworkX 设备上,我们将使用以下方式读取文件:

File.ReadAllBytes(filename);

但如果我们在内存量小得多的 NetDuino Plus 上尝试这样做,
我们只是得到一个 OutOfMemoryException 异常。

这些文件不是那么大,但我想在这种情况下这都是相对的(最大 1.5kb)。

在这样的设备上读取文件的正确方法是什么?

On a ChipworkX device we would read files using:

File.ReadAllBytes(filename);

But if we try that on a NetDuino Plus which has a much smaller amount of memory,
we simply get an OutOfMemoryException.

The files are not that big, but I guess that's all relative in this case (1.5kb max).

What's the correct way to read files on a device like this?

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

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

发布评论

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

评论(3

笛声青案梦长安 2024-10-15 02:46:49

使用 FileStream

using (var fileStream = new FileStream(filename, FileMode.Open))
{
    byte[] block = new byte[1024];
    int readLength;
    while ((readLength = fileStream.Read(block, 0, block.Length)) > 0)
    {
        Process(block, readLength);
    }
}

编写您自己的 处理方法。 1024 的块长度只是一个示例,一次读取尽可能大的块。您可以根据数据进行更改。

Use a FileStream

using (var fileStream = new FileStream(filename, FileMode.Open))
{
    byte[] block = new byte[1024];
    int readLength;
    while ((readLength = fileStream.Read(block, 0, block.Length)) > 0)
    {
        Process(block, readLength);
    }
}

write your own Process method. The block length of 1024 is just an example, read as big chunks as you can process at a time. You can vary that depending on the data.

属性 2024-10-15 02:46:49

我假设您认为应该有足够的内存。如果是这样,我怀疑内部默认缓冲区大小会造成问题。尝试在打开文件时明确指定缓冲区大小,以使其紧贴实际文件长度:

string path = //some path
byte[] buffer;
int bufferSize = (int)new FileInfo(path).Length;

using (FileStream fs = new FileStream(
    path, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize))
{
    buffer = new byte[bufferSize];

    fs.Read(buffer, 0, buffer.Length);
}

//do stuff with buffer 

I am assuming that you believe that there should be sufficient memory. If this is so, I suspect that internal default buffer sizes are blowing things. Try explicitly stating buffer sizes when opening the file to keep it tight to the actual file length:

string path = //some path
byte[] buffer;
int bufferSize = (int)new FileInfo(path).Length;

using (FileStream fs = new FileStream(
    path, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize))
{
    buffer = new byte[bufferSize];

    fs.Read(buffer, 0, buffer.Length);
}

//do stuff with buffer 
定格我的天空 2024-10-15 02:46:49

当您使用内存有限的设备时,最好使用扇区大小的缓冲区。你所做的就是用速度换取内存。当你的内存很少时,你必须放慢速度,而扇区是你可以使用的、有意义的最小单位。

我建议使用 512 字节的缓冲区。

When you are using a device with limited memory, it is a good idea to use a buffer that is the size of a sector. What you are doing is trading speed for memory. When you have little memory, you must do things more slowly and a sector is the smallest unit you can use that makes any sense.

I suggest a buffer of 512 bytes.

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