🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
这些对象是通过 `require("Storage").open` 创建的,并允许对存储项进行读/写操作。 存储库写入闪存(只能按块擦除),并且与普通文件系统不同,它在一个长的连续区域中分配文件,以便可以从 Espruino 轻松访问它们。 这给 `StorageFile` 带来了挑战,因为它允许你向文件追加内容,所以取而代之的是,`StorageFile` 将文件存储在块中。它使用文件名的最后一个字符来表示块编号(例如,"foobar\\1","foobar\\2" 等)。 这意味着虽然 `StorageFile` 文件与来自 `Storage` 的那些文件存在于同一区域中,但它们应该使用 `Storage.open`(而不是 `Storage.read`)来读取。 ~~~ f = require("Storage").open("foobar","w"); f.write("Hell"); f.write("o World\n"); f.write("Hello\n"); f.write("World 2\n"); f.write("Hello World 3\n"); // there's no need to call 'close' // then f = require("Storage").open("foobar","r"); f.read(13) // "Hello World\nH" f.read(13) // "ello\nWorld 2\n" f.read(13) // "Hello World 3" f.read(13) // "\n" f.read(13) // undefined // or f = require("Storage").open("foobar","r"); f.readLine() // "Hello World\n" f.readLine() // "Hello\n" f.readLine() // "World 2\n" f.readLine() // "Hello World 3\n" f.readLine() // undefined // now get rid of file f.erase(); ~~~ 注意:存储文件利用已擦除的闪存的所有位都是 1 这一事实来检测一个文件的末尾。因此,你不应该向这些文件写入字符代码 255(`"\xFF"`)。 [TOC]