FileSystemManager.truncate
收藏
我的收藏

基础库 2.60.0 开始支持本方法,低版本需做兼容处理,这是一个异步方法。

对文件内容进行截断操作,只有用户目录(ttfile://user)下的文件可以截断。

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

语法

FileSystemManager.truncate(options)

参数说明

options 为 object 类型,属性如下:

属性名类型默认值必填说明最低支持版本
lengthnumber0
截断位置,默认 0。如果 length 小于文件长度(字节),则只有前面 length 个字节会保留在文件中,其余内容会被删除;如果 length 大于文件长度,则会对其进行扩展,并且扩展部分将填充空字节('\0')
2.60.0
filePathstring
要截断的文件路径, 必须以 ttfile://user 开头
2.60.0
successfunction
接口调用成功的回调函数
2.60.0
failfunction
接口调用失败的回调函数
2.60.0
completefunction
接口调用结束的回调函数(调用成功、失败都会执行)
2.60.0

回调成功

object 类型,属性如下:

属性名类型说明最低支持版本
errMsgstring
"truncate:ok"
2.60.0

回调失败

object 类型,属性如下:

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

错误码

errNoerrMsg说明最低支持版本
20000truncate:fail filePath is required
filePath 参数错误
2.60.0
21101truncate:fail permission denied, truncate %s
文件路径不可写
2.60.0
21102truncate:fail no such file or directory, truncate %s
filePath 文件不存在
2.60.0
21103truncate:fail user dir saved file size limit exceeded
超出目录大小限制
2.60.0
21104truncate:fail operation not permitted, truncate %s
filePath 类型不是文件
2.60.0

扫码体验

请使用字节宿主APP扫码

代码示例

开发者工具中预览

const fs = tt.getFileSystemManager()

fs.truncate({
  filePath: `ttfile://user/example.txt`,
  length: 10,
  success(res) {
    console.log(res)
  },
  fail(res) {
    console.error(res)
  },
})

// 同步接口
try {
  fs.truncateSync(`ttfile://user/example.txt`, 10)
} catch (e) {
  console.error(e)
}