FileSystemManager.truncateSync
收藏
我的收藏

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

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

语法

FileSystemManager.truncateSync(filePath[, length])

参数说明

filePath

类型默认值必填说明最低支持版本
string要截断的文件路径, 必须以 ttfile://user 开头2.60.0

length

类型默认值必填说明最低支持版本
number0截断位置,默认 0。如果 length 小于文件长度(字节),则只有前面 length 个字节会保留在文件中,其余内容会被删除;如果 length 大于文件长度,则会对其进行扩展,并且扩展部分将填充空字节('\0')2.60.0

返回值

错误说明

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

Bug & Tip