小程序开放平台

文档中心
暂无目录

fs.appendFileSync

更新时间:2025-07-23 11:41:17

📌 从基础库 3.125.4 版本开始支持

功能描述

FileSystemManager.appendFile
的同步版本

参数

参数
类型
说明
filePathstring要追加内容的文件路径 (本地路径)
datastring | ArrayBuffer要追加的文本或二进制数据
encodingstring指定写入文件的字符编码

encoding 的合法值(小红书平台仅支持以下编码格式)

说明
utf-8UTF-8 编码
utf8UTF-8 编码(别名)
binary二进制格式

示例代码

javascript
const fs = xhs.getFileSystemManager()

// 异步调用
fs.appendFile({
  filePath: `${xhs.env.USER_DATA_PATH}/hello.txt`,
  data: 'some text',
  encoding: 'utf8',
  success(res) {
    console.log(res)
  },
  fail(res) {
    console.error(res)
  }
})

// 同步接口
try {
  fs.appendFileSync(`${xhs.env.USER_DATA_PATH}/hello.txt`, 'some text', 'utf8')
} catch(e) {
  console.error(e)
}