💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
## Call type:呼叫类型: `require("Storage").write(name,data,offset,size)` ## 参数 `name` - 文件名 - 最多 28 个字符(区分大小写) `data` - 要写入的数据 `offset` - [可选] 文件中要写入的偏移量(如果`0`创建了新文件,`undefined`否则 Espruino 会尝试在现有文件中写入(如果存在) `size` - [可选] 文件的大小(如果要创建的文件大于数据) ## 返回 成功为真,失败为假 ## 描述 在闪存区域中写入/创建文件。这是非易失性的,当设备复位或断电时不会消失。 简单地写 `require("Storage").write("MyFile","Some data")` 来写入一个新文件,以及`require("Storage").read("MyFile")`来读取它。 如果您提供: * 一个字符串,它将按原样被写入。 * 一个数组,将被作为一个字节数组写入(但读取回来时是作为一个字符串)。 * 一个对象,在被写入之前它将自动被转换为一个 JSON 字符串。 **注意**:如果提供了一个数组,它将不会被转换为 JSON。为了明确这种转换,你可以使用 [Storage.writeJSON](writeJSON.md) 你也可以创建一个文件然后稍后填充数据**只要你不尝试覆盖已经存在的数据**。例如: ~~~ var f = require("Storage"); f.write("a","Hello",0,14); // Creates a new file, 14 chars long print(JSON.stringify(f.read("a"))); // read the file // any nonwritten chars will be char code 255: "Hello\u00FF\u00FF\u00FF\u00FF\u00FF\u00FF\u00FF\u00FF\u00FF" f.write("a"," ",5); // write within the file f.write("a","World!!!",6); // write again within the file print(f.read("a")); // "Hello World!!!" f.write("a"," ",0); // Writing to location 0 again will cause the file to be re-written print(f.read("a")); // " " ~~~ 如果要写入的数据多于可用 RAM,则此方法非常有用 - 例如,Web IDE 使用此方法将大文件写入板载存储。 **注意:** 这个函数应该用于普通文件,而不是用`require("Storage").open(文件名,...)` 创建的 [StorageFile](../StorageFile.md)