# undefined FileSystemManager.truncateSync(Object object)
基础库 2.16.1 开始支持,低版本需做兼容处理。
小程序插件:支持,需要小程序基础库版本不低于 2.19.2
对文件内容进行截断操作 (truncate 的同步版本)
# 参数
# Object object
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
filePath | string | 是 | 要截断的文件路径 (本地路径) | |
length | number | 0 | 否 | 截断位置,默认0。如果 length 小于文件长度(字节),则只有前面 length 个字节会保留在文件中,其余内容会被删除;如果 length 大于文件长度,则会对其进行扩展,并且扩展部分将填充空字节('\0') |
# 返回值
# undefined
# 错误
错误码 | 错误信息 | 说明 |
---|---|---|
fail no such file or directory, open ${filePath} | 指定的 filePath 所在目录不存在 | |
fail illegal operation on a directory, open "${filePath}" | 指定的 filePath 是一个已经存在的目录 | |
fail permission denied, open ${dirPath} | 指定的 filePath 路径没有写权限 | |
fail the maximum size of the file storage limit is exceeded | 存储空间不足 | |
fail sdcard not mounted | android sdcard 挂载失败 |
# 示例代码
const fs = wx.getFileSystemManager()
fs.truncateSync({
filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
length: 10, // 从第10个字节开始截断
})