抖音开放平台Logo
开发者文档
“/”唤起搜索
控制台

FileSystemManager.write

收藏
我的收藏

基础库 3.74.0 开始支持本方法,这是一个异步方法。

写入文件

前提条件
业务背景
使用限制
注意事项
相关教程

语法

FileSystemManager.write(options)

参数说明

options 为 object 类型,属性如下:

属性名类型默认值必填说明最低支持版本
lengthnumber
只在 data 类型是 ArrayBuffer 时有效,指定要写入的字节数,默认为 ArrayBuffer 从0开始偏移 offset 个字节后剩余的字节数
3.74.0
positionnumber
指定文件开头的偏移量,即数据要被写入的位置。当 position 不传或者传入非 Number 类型的值时,数据会被写入当前指针所在位置
3.74.0
datastring | Arraybuffer
写入的内容,类型为 String 或 ArrayBuffer
3.74.0
fdstring
文件描述符。fd 通过 FileSystemManager.open 或 FileSystemManager.openSync 接口获得
3.74.0
encodingenumutf8
只在 data 类型是 String 时有效,指定写入文件的字符编码,默认为 utf8
3.74.0
offsetnumber0
只在 data 类型是 ArrayBuffer 时有效,决定 ArrayBuffer 中要被写入的部位,即 ArrayBuffer 中的索引,默认0
3.74.0
successfunction
接口调用成功的回调函数
3.74.0
failfunction
接口调用失败的回调函数
3.74.0
completefunction
接口调用结束的回调函数(调用成功、失败都会执行)
3.74.0

encoding 的合法值

说明最低支持版本
ascii
ascii
3.74.0
base64
base64
3.74.0
binary
binary
3.74.0
hex
hex
3.74.0
ucs2
以小端序读取
3.74.0
ucs-2
以小端序读取
3.74.0
utf16le
以小端序读取
3.74.0
utf-16le
以小端序读取
3.74.0
utf-8
utf-8
3.74.0
utf8
utf8
3.74.0
latin1
latin1
3.74.0

回调成功

object 类型,属性如下:

属性名类型说明最低支持版本
errMsgstring
"FileSystemManager.write:ok"
3.74.0
bytesWrittennumber
实际被写入到文件中的字节数(被写入的字节数不一定与被写入的字符串字符数相同)
3.74.0

回调失败

object 类型,属性如下:

属性名类型说明最低支持版本
errMsgstring
"FileSystemManager.write:fail" + 详细错误信息
3.74.0

错误码

errNoerrMsg说明最低支持版本
20001invalid param
参数错误
3.74.0
21100bad file descriptor
3.74.0
21101system error
小游戏框架内部错误,有需要请创建工单咨询
3.74.0
21102the maximum size of the file storage limit is exceeded
3.74.0
21103${encoding} encode error
3.74.0
21104data to write is empty
3.74.0
21105illegal operation on a directory
3.74.0
21106value of length is out of range
3.74.0
21107value of offset is out of range
3.74.0
21108invalid fd
3.74.0

扫码体验

请使用字节宿主APP扫码

代码示例

开发者工具中预览

const fs = tt.getFileSystemManager(); const fd = fs.openSync({ filePath, flag: "w+", }); fs.write({ fd, data: '测试写入的数据', offset: 0, length: 10, position: 0, success: (res) => { console.log('文件写入成功', res); }, fail: (err) => { console.error('文件写入失败', err); } });