TikTok for Developers

开发者文档

上次更新时间:2026年11月25日

文件

授予访问持久化文件系统管理器的权限,用于在小游戏沙盒存储中读取、写入、复制和管理本地文件及目录。

TTMinis.game.getFileSystemManager()

获取全局唯一的文件系统管理器。

返回值

FileSystemManager - 文件系统管理器实例。

示例

const fs = TTMinis.game.getFileSystemManager();

FileSystemManager

FileSystemManager.readFile(opts)

读取本地文件内容。

参数

字段

类型

描述

是否必填

filePath

string

要读取的文件路径(本地路径)

是

encoding

string

指定读取文件时使用的字符编码。如果不提供encoding,则文件内容以二进制形式(ArrayBuffer 格式)读取。

否

position

int32

开始读取的位置。如果不指定,则从文件开头开始读取。有效范围为 [0, fileLength - 1]。单位:字节。

否

length

int32

要读取的字节数。如果不指定,则读取至文件末尾。有效范围:[1, fileLength]。单位:字节。

否

success

function

API 调用成功时的回调函数

否

fail

function

API 调用失败时的回调函数

否

complete

function

API 调用结束时的回调函数(无论成功或失败均执行)

否

success 回调会收到包含以下字段的结果对象:

字段

类型

描述

data

string|ArrayBuffer

文件内容。返回 string 或 ArrayBuffer 类型。

errMsg

string

错误信息。成功时返回 "readFile:ok"。

示例

const fs = TTMinis.game.getFileSystemManager();

fs.readFile({
  filePath: `${TTMinis.game.env.USER_DATA_PATH}/hello.txt`,
  encoding: 'utf8',
  position: 0,
  success(res) {
    console.log(res.data)
  },
  fail(res) {
    console.error(res)
  }
})

FileSystemManager.readFileSync(filePath, encoding, position, length)

同步读取本地文件内容。接受与 readFile 相同的参数,但以位置参数形式传入:filePath(字符串)、encoding(字符串,可选)、position(int32,可选)以及 length(int32,可选)。

返回值

string| ArrayBuffer - 文件内容。

示例

const fs = TTMinis.game.getFileSystemManager();

try {
  const res = fs.readFileSync(`${TTMinis.game.env.USER_DATA_PATH}/hello.txt`, 'utf8', 0)
  console.log(res)
} catch(e) {
  console.error(e)
}

FileSystemManager.access(opts)

检查文件或目录是否存在。

参数

字段

类型

描述

是否必填

path

string

要检查的文件或目录路径(本地路径)

是

success

function

API 调用成功时的回调函数

否

fail

function

API 调用失败时的回调函数

否

complete

function

API 调用结束时的回调函数(无论成功或失败均执行)

否

示例

const fs = TTMinis.game.getFileSystemManager();

fs.access({
  path: `${TTMinis.game.env.USER_DATA_PATH}/hello.txt`,
  success(res) {
    // File exists
    console.log(res)
  },
  fail(res) {
    // File does not exist or other error
    console.error(res)
  }
})

FileSystemManager.accessSync(path)

同步检查文件或目录是否存在。如果路径不存在,则抛出异常。

参数

字段

类型

描述

是否必填

path

string

要检查的文件或目录路径(本地路径)

是

示例

const fs = TTMinis.game.getFileSystemManager();

try {
  fs.accessSync(`${TTMinis.game.env.USER_DATA_PATH}/hello.txt`)
  console.log('File exists')
} catch(e) {
  console.error(e)
}

FileSystemManager.writeFile(opts)

向文件写入数据。

参数

字段

类型

描述

是否必填

filePath

string

要写入的文件路径(本地路径)

是

data

string|ArrayBuffer

要写入的文本或二进制数据

是

encoding

string

指定写入文件时使用的字符编码。默认值:utf8。

否

success

function

API 调用成功时的回调函数

否

fail

function

API 调用失败时的回调函数

否

complete

function

API 调用结束时的回调函数(无论成功或失败均执行)

否

示例

const fs = TTMinis.game.getFileSystemManager();

fs.writeFile({
  filePath: `${TTMinis.game.env.USER_DATA_PATH}/hello.txt`,
  data: 'some text or arrayBuffer',
  encoding: 'utf8',
  success(res) {
    console.log(res)
  },
  fail(res) {
    console.error(res)
  }
})

FileSystemManager.writeFileSync(filePath, data, encoding)

同步写入数据到文件。接受与 writeFile 相同的参数,但以位置参数形式传入:filePath(字符串)、data(字符串或 ArrayBuffer)以及 encoding(字符串,可选)。

示例

const fs = TTMinis.game.getFileSystemManager();

try {
  const res = fs.writeFileSync(
    `${TTMinis.game.env.USER_DATA_PATH}/hello.txt`,
    'some text or arrayBuffer',
    'utf8'
  )
  console.log(res)
} catch(e) {
  console.error(e)
}

FileSystemManager.mkdir(opts)

创建目录。

参数

字段

类型

描述

是否必填

dirPath

string

要创建的目录路径(本地路径)

是

recursive

Boolean

true:在创建目标目录前递归创建父目录。如果父目录已存在,则不会重复创建。默认值:false。

否

success

function

API 调用成功时的回调函数

否

fail

function

API 调用失败时的回调函数

否

complete

function

API 调用结束时的回调函数(无论成功或失败均执行)

否

示例

const fs = TTMinis.game.getFileSystemManager();

fs.mkdir({
  dirPath: `${TTMinis.game.env.USER_DATA_PATH}/example`,
  recursive: false,
  success(res) {
    console.log(res)
  },
  fail(res) {
    console.error(res)
  }
})

FileSystemManager.mkdirSync(dirPath, recursive)

同步创建目录。接受 dirPath(字符串)和 recursive(布尔值,可选)作为位置参数。

示例

const fs = TTMinis.game.getFileSystemManager();

try {
  fs.mkdirSync(`${TTMinis.game.env.USER_DATA_PATH}/example`, false)
  console.log('directory created')
} catch(e) {
  console.error(e)
}

FileSystemManager.copyFile(opts)

复制文件。

参数

字段

类型

描述

是否必填

srcPath

string

源文件路径。支持本地路径。

是

destPath

string

目标文件路径。支持本地路径。

是

success

function

API 调用成功时的回调函数

否

fail

function

API 调用失败时的回调函数

否

complete

function

API 调用结束时的回调函数(无论成功或失败均执行)

否

示例

const fs = TTMinis.game.getFileSystemManager();

fs.copyFile({
  srcPath: `${TTMinis.game.env.USER_DATA_PATH}/hello.txt`,
  destPath: `${TTMinis.game.env.USER_DATA_PATH}/hello_copy.txt`,
  success(res) {
    console.log(res)
  },
  fail(res) {
    console.error(res)
  }
})

FileSystemManager.copyFileSync(srcPath, destPath)

同步复制文件。接受 srcPath(字符串)和 destPath(字符串)作为位置参数。

示例

const fs = TTMinis.game.getFileSystemManager();

try {
  fs.copyFileSync(
    `${TTMinis.game.env.USER_DATA_PATH}/hello.txt`,
    `${TTMinis.game.env.USER_DATA_PATH}/hello_copy.txt`
  )
} catch(e) {
  console.error(e)
}

FileSystemManager.unzip(opts)

解压文件。

参数

字段

类型

描述

是否必填

zipFilePath

string

源文件路径。支持本地路径。必须为 zip 压缩包。

是

targetPath

string

目标目录路径。支持本地路径。

是

success

function

API 调用成功时的回调函数

否

fail

function

API 调用失败时的回调函数

否

complete

function

API 调用结束时的回调函数(无论成功或失败均执行)

否

FileSystemManager.open(opts)

打开文件并返回文件描述符。

注意:从 SDK 版本 0.15.0 起开始支持。低版本需进行兼容性适配。

参数

字段

类型

描述

是否必填

filePath

string

文件路径(本地路径)

是

flag

string

文件系统标志。默认值:r。

否

success

function

API 调用成功时的回调函数

否

fail

function

API 调用失败时的回调函数

否

complete

function

API 调用结束时的回调函数(无论成功或失败均执行)

否

flag 有效值:

值

描述

a

以追加模式打开文件。如果文件不存在,则创建文件。

ax

与 a 类似,但如果路径已存在,则操作失败

a+

以读取和追加模式打开文件。如果文件不存在,则创建文件。

ax+

与 a+ 类似,但如果路径已存在,则操作失败

as

以同步模式打开文件进行追加。如果文件不存在,则创建文件。

as+

以同步模式打开文件进行读取和追加。如果文件不存在,则创建文件。

r

以读取模式打开文件。如果文件不存在,则抛出异常。

r+

以读写模式打开文件。如果文件不存在,则抛出异常。

w

以写入模式打开文件。如果文件不存在,则创建文件。如果文件存在,则截断文件。

wx

与 w 类似,但如果路径已存在,则操作失败

w+

以读写模式打开文件。如果文件不存在,则创建文件。如果文件存在,则截断文件。

wx+

与 w+ 类似,但如果路径已存在,则操作失败

success 回调会收到包含以下字段的结果对象:

字段

类型

描述

fd

string

文件描述符

示例

const fs = TTMinis.game.getFileSystemManager();

fs.open({
  filePath: `${TTMinis.game.env.USER_DATA_PATH}/hello.txt`,
  flag: 'a+',
  success(res) {
    console.log(res.fd)
  },
  fail(res) {
    console.error(res)
  }
})

FileSystemManager.openSync(opts)

同步打开文件并返回文件描述符。接受与 open 相同的参数,包括 filePath(字符串,必填)和 flag(字符串,可选,默认值为 r)。

说明:从 SDK 版本 0.15.0 起开始支持。低版本需进行兼容性适配。

返回值

string - 文件描述符。

示例

const fs = TTMinis.game.getFileSystemManager();

try {
  const fd = fs.openSync({
    filePath: `${TTMinis.game.env.USER_DATA_PATH}/hello.txt`,
    flag: 'a+'
  })
  console.log(fd)
} catch(e) {
  console.error(e)
}

FileSystemManager.close(opts)

关闭文件。

注意:从 SDK 版本 0.15.0 起开始支持。低版本需进行兼容性适配。

参数

字段

类型

描述

是否必填

fd

string

要关闭的文件描述符。通过 FileSystemManager.open 或 FileSystemManager.openSync 获取。

是

success

function

API 调用成功时的回调函数

否

fail

function

API 调用失败时的回调函数

否

complete

function

API 调用结束时的回调函数(无论成功或失败均执行)

否

示例

const fs = TTMinis.game.getFileSystemManager();

fs.open({
  filePath: `${TTMinis.game.env.USER_DATA_PATH}/hello.txt`,
  flag: 'a+',
  success(res) {
    fs.close({
      fd: res.fd,
      success(res) {
        console.log('file closed')
      },
      fail(res) {
        console.error(res)
      }
    })
  }
})

FileSystemManager.closeSync(opts)

同步关闭文件。接受 fd(字符串,必填)作为参数对象。

注意:从 SDK 版本 0.15.0 起开始支持。低版本需进行兼容性适配。

示例

const fs = TTMinis.game.getFileSystemManager();

try {
  const fd = fs.openSync({
    filePath: `${TTMinis.game.env.USER_DATA_PATH}/hello.txt`,
    flag: 'a+'
  })
  fs.closeSync({ fd: fd })
  console.log('file closed')
} catch(e) {
  console.error(e)
}

FileSystemManager.read(opts)

使用文件描述符读取文件内容。

注意:从 SDK 版本 0.15.0 起开始支持。低版本需进行兼容性适配。

参数

字段

类型

描述

是否必填

fd

string

文件描述符。通过 FileSystemManager.open 或 FileSystemManager.openSync 获取。

是

arrayBuffer

ArrayBuffer

用于写入数据的缓冲区。必须为 ArrayBuffer 实例。

是

offset

int32

缓冲区中的写入偏移量。默认值:0。

否

length

int32

从文件中读取的字节数。默认值:0。

否

position

int32

读取的起始位置。如果不提供或为 null,则从当前文件指针位置读取。如果为正整数,则文件指针位置保持不变,从 position 处开始读取。

否

success

function

API 调用成功时的回调函数

否

fail

function

API 调用失败时的回调函数

否

complete

function

API 调用结束时的回调函数(无论成功或失败均执行)

否

success 回调会收到包含以下字段的结果对象:

字段

类型

描述

bytesRead

int32

实际读取的字节数

arrayBuffer

ArrayBuffer

被写入的缓冲区对象,即 arrayBuffer 输入参数

示例

const fs = TTMinis.game.getFileSystemManager();
const ab = new ArrayBuffer(1024);

fs.open({
  filePath: `${TTMinis.game.env.USER_DATA_PATH}/hello.txt`,
  flag: 'a+',
  success(res) {
    fs.read({
      fd: res.fd,
      arrayBuffer: ab,
      length: 10,
      success(res) {
        console.log(res)
      },
      fail(res) {
        console.error(res)
      }
    })
  }
})

FileSystemManager.readSync(opts)

使用文件描述符同步读取文件内容。接受与 read 相同的参数(不包含 success、fail 和 complete 回调)。

注意:从 SDK 版本 0.15.0 起开始支持。低版本需进行兼容性适配。

返回值

ReadResult 对象,包含以下字段:

字段

类型

描述

bytesRead

int32

实际读取的字节数

arrayBuffer

ArrayBuffer

被写入的缓冲区对象,即 arrayBuffer 输入参数

FileSystemManager.write(opts)

使用文件描述符向文件写入数据。

注意:从 SDK 版本 0.15.0 起开始支持。低版本需进行兼容性适配。

参数

字段

类型

描述

是否必填

fd

string

文件描述符。通过 FileSystemManager.open 或 FileSystemManager.openSync 获取。

是

data

string|ArrayBuffer

要写入的内容。类型为 string 或 ArrayBuffer。

是

offset

int32

仅当 data 为 ArrayBuffer 时有效。决定写入数据在 ArrayBuffer 内的偏移量。默认值:0。

否

length

int32

仅当 data 为 ArrayBuffer 时有效。指定要写入的字节数。默认值:ArrayBuffer 中 offset 后剩余的字节数。

否

encoding

string

仅当 data 为字符串时有效。指定写入时使用的字符编码。默认值:utf8。

否

position

int32

指定从文件起始位置起,要写入数据的偏移位置。如果不提供或不是数字,则在当前文件指针位置写入数据。

否

success

function

API 调用成功时的回调函数

否

fail

function

API 调用失败时的回调函数

否

complete

function

API 调用结束时的回调函数(无论成功或失败均执行)

否

success 回调会收到包含以下字段的结果对象:

字段

类型

描述

bytesWritten

int32

实际写入文件的字节数。请注意,写入的字节数可能与字符串中的字符数不相等。

示例

const fs = TTMinis.game.getFileSystemManager();

fs.open({
  filePath: `${TTMinis.game.env.USER_DATA_PATH}/hello.txt`,
  flag: 'a+',
  success(res) {
    fs.write({
      fd: res.fd,
      data: 'some text',
      success(res) {
        console.log(res.bytesWritten)
      },
      fail(res) {
        console.error(res)
      }
    })
  }
})

FileSystemManager.writeSync(opts)

使用文件描述符同步写入数据到文件。接受与 write 相同的参数(不包含 success、fail 和 complete 回调)。

注意:从 SDK 版本 0.15.0 起开始支持。低版本需进行兼容性适配。

返回值

WriteResult 对象,包含以下字段:

字段

类型

描述

bytesWritten

int32

实际写入文件的字节数

示例

const fs = TTMinis.game.getFileSystemManager();

try {
  const fd = fs.openSync({
    filePath: `${TTMinis.game.env.USER_DATA_PATH}/hello.txt`,
    flag: 'a+'
  })
  const res = fs.writeSync({
    fd: fd,
    data: 'some text'
  })
  console.log(res.bytesWritten)
} catch(e) {
  console.error(e)
}

FileSystemManager.rmdir(opts)

移除目录。

注意:从 SDK 版本 0.15.0 起开始支持。低版本需进行兼容性适配。

参数

字段

类型

描述

是否必填

dirPath

string

要移除的目录路径(本地路径)

是

recursive

Boolean

true:递归移除目录及其所有子目录和文件。默认值:false。

否

success

function

API 调用成功时的回调函数

否

fail

function

API 调用失败时的回调函数

否

complete

function

API 调用结束时的回调函数(无论成功或失败均执行)

否

示例

const fs = TTMinis.game.getFileSystemManager();

fs.rmdir({
  dirPath: `${TTMinis.game.env.USER_DATA_PATH}/example`,
  recursive: false,
  success(res) {
    console.log(res)
  },
  fail(res) {
    console.error(res)
  }
})

FileSystemManager.rmdirSync(dirPath, recursive)

同步移除目录。接受 dirPath(字符串)和 recursive(布尔值,可选)作为位置参数。

注意:从 SDK 版本 0.15.0 起开始支持。低版本需进行兼容性适配。

示例

const fs = TTMinis.game.getFileSystemManager();

try {
  fs.rmdirSync(`${TTMinis.game.env.USER_DATA_PATH}/example`, false)
  console.log('directory removed')
} catch(e) {
  console.error(e)
}

FileSystemManager.readdir(opts)

读取目录下的文件列表。

注意:从 SDK 版本 0.15.0 起开始支持。低版本需进行兼容性适配。

参数

字段

类型

描述

是否必填

dirPath

string

要读取的目录路径(本地路径)

是

success

function

API 调用成功时的回调函数

否

fail

function

API 调用失败时的回调函数

否

complete

function

API 调用结束时的回调函数(无论成功或失败均执行)

否

success 回调会收到包含以下字段的结果对象:

字段

类型

描述

files

list<string>

指定目录下的文件名数组

示例

const fs = TTMinis.game.getFileSystemManager();

fs.readdir({
  dirPath: `${TTMinis.game.env.USER_DATA_PATH}/example`,
  success(res) {
    console.log(res.files)
  },
  fail(res) {
    console.error(res)
  }
})

FileSystemManager.readdirSync(dirPath)

同步读取目录下的文件列表。接受 dirPath(字符串)作为位置参数。

注意:从 SDK 版本 0.15.0 起开始支持。低版本需进行兼容性适配。

返回值

list<string> - 指定目录下的文件名数组。

示例

const fs = TTMinis.game.getFileSystemManager();

try {
  const res = fs.readdirSync(`${TTMinis.game.env.USER_DATA_PATH}/example`)
  console.log(res)
} catch(e) {
  console.error(e)
}

FileSystemManager.unlink(opts)

删除文件。

注意:从 SDK 版本 0.15.0 起开始支持。低版本需进行兼容性适配。

参数

字段

类型

描述

是否必填

filePath

string

要删除的文件路径(本地路径)

是

success

function

API 调用成功时的回调函数

否

fail

function

API 调用失败时的回调函数

否

complete

function

API 调用结束时的回调函数(无论成功或失败均执行)

否

示例

const fs = TTMinis.game.getFileSystemManager();

fs.unlink({
  filePath: `${TTMinis.game.env.USER_DATA_PATH}/hello.txt`,
  success(res) {
    console.log(res)
  },
  fail(res) {
    console.error(res)
  }
})

FileSystemManager.unlinkSync(filePath)

同步删除文件。接受 filePath(字符串)作为位置参数。

注意:从 SDK 版本 0.15.0 起开始支持。低版本需进行兼容性适配。

示例

const fs = TTMinis.game.getFileSystemManager();

try {
  fs.unlinkSync(`${TTMinis.game.env.USER_DATA_PATH}/hello.txt`)
  console.log('file removed')
} catch(e) {
  console.error(e)
}

FileSystemManager.stat(opts)

获取文件的 Stats 对象。

注意:从 SDK 版本 0.16.0 开始支持。低版本需进行兼容性适配。

参数

字段

类型

描述

是否必填

path

string

文件或目录路径(本地路径)

是

recursive

Boolean

如果值为 true,则递归获取目录下每个文件的 Stats 信息。默认值:false。

否

success

function

API 调用成功时的回调函数

否

fail

function

API 调用失败时的回调函数

否

complete

function

API 调用结束时的回调函数(无论成功或失败均执行)

否

success 回调会收到包含以下字段的结果对象:

字段

类型

描述

stats

Stats|list<FileStats>

当 recursive 为 false 时,stats 是一个 Stats 对象。当 recursive 为 true 且 path 是目录时,stats 是一个 FileStats 对象数组,每个对象包含 path 和 stats。

Stats 对象

字段

类型

描述

mode

string

文件类型和访问权限。对应 POSIX 的 stat.st_mode。

size

int32

文件大小(字节)。对应 POSIX 的 stat.st_size。

lastAccessedTime

int32

文件的最后访问或执行时间。UNIX 时间戳。对应 POSIX 的 stat.st_atime。

lastModifiedTime

int32

文件的最后修改时间。UNIX 时间戳。对应 POSIX 的 stat.st_mtime。

Stats 对象方法:

方法

返回类型

描述

isDirectory()

Boolean

检查文件是否为目录

isFile()

Boolean

检查文件是否为常规文件

示例

const fs = TTMinis.game.getFileSystemManager();

// recursive is false
fs.stat({
  path: `${TTMinis.game.env.USER_DATA_PATH}/testDir`,
  success(res) {
    console.log(res.stats.isDirectory())
  },
  fail(res) {
    console.error(res)
  }
})

// recursive is true
fs.stat({
  path: `${TTMinis.game.env.USER_DATA_PATH}/testDir`,
  recursive: true,
  success(res) {
    Object.keys(res.stats).forEach(path => {
      let stats = res.stats[path]
      console.log(path, stats.isDirectory())
    })
  },
  fail(res) {
    console.error(res)
  }
})

FileSystemManager.statSync(path, recursive)

同步获取文件的 Stats 对象。接受 path(字符串)和 recursive(布尔值,可选)作为位置参数。

注意:从 SDK 版本 0.16.0 开始支持。低版本需进行兼容性适配。

返回值

Stats| list<FileStats> - 当 recursive 为 false 时,返回一个 Stats 对象。当 recursive 为 true 且 path 是目录时,返回一个数组。

示例

const fs = TTMinis.game.getFileSystemManager();

try {
  const stats = fs.statSync(`${TTMinis.game.env.USER_DATA_PATH}/testDir`, false)
  console.log(stats.isDirectory())
} catch(e) {
  console.error(e)
}

FileSystemManager.fstat(opts)

通过文件描述符获取文件的状态信息。

注意:从 SDK 版本 0.16.0 开始支持。低版本需进行兼容性适配。

参数

字段

类型

描述

是否必填

fd

string

文件描述符。通过 FileSystemManager.open 或 FileSystemManager.openSync 获取。

是

success

function

API 调用成功时的回调函数

否

fail

function

API 调用失败时的回调函数

否

complete

function

API 调用结束时的回调函数(无论成功或失败均执行)

否

success 回调会收到包含以下字段的结果对象:

字段

类型

描述

stats

Stats

包含文件状态信息的 Stats 对象

示例

const fs = TTMinis.game.getFileSystemManager();

fs.open({
  filePath: `${TTMinis.game.env.USER_DATA_PATH}/hello.txt`,
  flag: 'a+',
  success(res) {
    fs.fstat({
      fd: res.fd,
      success(res) {
        console.log(res.stats)
      },
      fail(res) {
        console.error(res)
      }
    })
  }
})

FileSystemManager.fstatSync(opts)

通过文件描述符同步获取文件的状态信息。接受一个包含 fd(字符串,必填)的参数对象。

注意:从 SDK 版本 0.16.0 开始支持。低版本需进行兼容性适配。

返回值

Stats - 包含文件状态信息的 Stats 对象。

示例

const fs = TTMinis.game.getFileSystemManager();

try {
  const fd = fs.openSync({
    filePath: `${TTMinis.game.env.USER_DATA_PATH}/hello.txt`,
    flag: 'a+'
  })
  const stats = fs.fstatSync({ fd: fd })
  console.log(stats)
} catch(e) {
  console.error(e)
}

FileSystemManager.rename(opts)

重命名文件。将文件从 oldPath 移动到 newPath。

注意:从 SDK 版本 0.16.0 开始支持。低版本需进行兼容性适配。

参数

字段

类型

描述

是否必填

oldPath

string

源文件路径。支持本地路径。

是

newPath

string

新文件路径。支持本地路径。

是

success

function

API 调用成功时的回调函数

否

fail

function

API 调用失败时的回调函数

否

complete

function

API 调用结束时的回调函数(无论成功或失败均执行)

否

示例

const fs = TTMinis.game.getFileSystemManager();

fs.rename({
  oldPath: `${TTMinis.game.env.USER_DATA_PATH}/hello.txt`,
  newPath: `${TTMinis.game.env.USER_DATA_PATH}/hello_new.txt`,
  success(res) {
    console.log(res)
  },
  fail(res) {
    console.error(res)
  }
})

FileSystemManager.renameSync(oldPath, newPath)

同步重命名文件。接受 oldPath(字符串)和 newPath(字符串)作为位置参数。

注意:从 SDK 版本 0.16.0 开始支持。低版本需进行兼容性适配。

示例

const fs = TTMinis.game.getFileSystemManager();

try {
  fs.renameSync(
    `${TTMinis.game.env.USER_DATA_PATH}/hello.txt`,
    `${TTMinis.game.env.USER_DATA_PATH}/hello_new.txt`
  )
  console.log('rename success')
} catch(e) {
  console.error(e)
}

FileSystemManager.getFileInfo(opts)

获取本地临时文件或本地缓存文件的信息。

注意:从 SDK 版本 0.16.0 开始支持。低版本需进行兼容性适配。

参数

字段

类型

描述

是否必填

filePath

string

要读取的文件路径(本地路径)

是

digestAlgorithm

string

用于计算文件摘要的算法。默认值:md5。有效值:md5、sha1、sha256。

否

success

function

API 调用成功时的回调函数

否

fail

function

API 调用失败时的回调函数

否

complete

function

API 调用结束时的回调函数(无论成功或失败均执行)

否

success 回调会收到包含以下字段的结果对象:

字段

类型

描述

size

int32

文件大小(字节)

digest

string

使用指定方法计算的文件摘要digestAlgorithm

示例

const fs = TTMinis.game.getFileSystemManager();

fs.getFileInfo({
  filePath: `${TTMinis.game.env.USER_DATA_PATH}/hello.txt`,
  digestAlgorithm: 'md5',
  success(res) {
    console.log(res.size)
    console.log(res.digest)
  },
  fail(res) {
    console.error(res)
  }
})

FileSystemManager.saveFile(opts)

将临时文件保存到本地存储。本接口会移动临时文件,因此 tempFilePath 在调用成功后将不可用。

注意:从 SDK 版本 0.16.0 开始支持。低版本需进行兼容性适配。

参数

字段

类型

描述

是否必填

tempFilePath

string

临时文件路径(本地路径)

是

filePath

string

目标文件路径(本地路径)

否

success

function

API 调用成功时的回调函数

否

fail

function

API 调用失败时的回调函数

否

complete

function

API 调用结束时的回调函数(无论成功或失败均执行)

否

success 回调会收到包含以下字段的结果对象:

字段

类型

描述

savedFilePath

string

保存后的文件路径(本地路径)

示例

const fs = TTMinis.game.getFileSystemManager();

fs.saveFile({
  tempFilePath: '/tmp/tempFile.txt',
  filePath: `${TTMinis.game.env.USER_DATA_PATH}/savedFile.txt`,
  success(res) {
    console.log(res.savedFilePath)
  },
  fail(res) {
    console.error(res)
  }
})

FileSystemManager.saveFileSync(tempFilePath, filePath)

同步将临时文件保存到本地存储。接受 tempFilePath(字符串)和 filePath(字符串,非必填)作为位置参数。

注意:从 SDK 版本 0.16.0 开始支持。低版本需进行兼容性适配。

返回值

string - 保存后的文件路径(本地路径)。

示例

const fs = TTMinis.game.getFileSystemManager();

try {
  const savedFilePath = fs.saveFileSync(
    '/tmp/tempFile.txt',
    `${TTMinis.game.env.USER_DATA_PATH}/savedFile.txt`
  )
  console.log(savedFilePath)
} catch(e) {
  console.error(e)
}

FileSystemManager.getSavedFileList(opts)

获取已保存的本地缓存文件列表。

注意:从 SDK 版本 0.16.0 开始支持。低版本需进行兼容性适配。

参数

字段

类型

描述

是否必填

success

function

API 调用成功时的回调函数

否

fail

function

API 调用失败时的回调函数

否

complete

function

API 调用结束时的回调函数(无论成功或失败均执行)

否

success 回调会收到包含以下字段的结果对象:

字段

类型

描述

fileList

list<object>

文件对象数组

fileList 中每一项的结构如下:

字段

类型

描述

filePath

string

文件路径(本地路径)

size

int32

本地文件大小(字节)

createTime

int32

文件保存时的时间戳,单位为秒(自 1970 年 1 月 1 日 08:00:00 起)

示例

const fs = TTMinis.game.getFileSystemManager();

fs.getSavedFileList({
  success(res) {
    console.log(res.fileList)
  },
  fail(res) {
    console.error(res)
  }
})

FileSystemManager.removeSavedFile(opts)

移除已保存的本地缓存文件。

注意:从 SDK 版本 0.16.0 开始支持。低版本需进行兼容性适配。

参数

字段

类型

描述

是否必填

filePath

string

要移除的文件路径(本地路径)

是

success

function

API 调用成功时的回调函数

否

fail

function

API 调用失败时的回调函数

否

complete

function

API 调用结束时的回调函数(无论成功或失败均执行)

否

示例

const fs = TTMinis.game.getFileSystemManager();

fs.removeSavedFile({
  filePath: `${TTMinis.game.env.USER_DATA_PATH}/savedFile.txt`,
  success(res) {
    console.log(res)
  },
  fail(res) {
    console.error(res)
  }
})

FileSystemManager.appendFile(opts)

将内容追加到文件末尾。

注意:从 SDK 版本 0.18.0 开始支持。低版本需进行兼容性适配。

参数

字段

类型

描述

是否必填

filePath

string

要追加内容的文件路径(本地路径)

是

data

string|ArrayBuffer

要追加的文本或二进制数据

是

encoding

string

指定写入时使用的字符编码。默认值:utf8。

否

success

function

API 调用成功时的回调函数

否

fail

function

API 调用失败时的回调函数

否

complete

function

API 调用结束时的回调函数(无论成功或失败均执行)

否

示例

const fs = TTMinis.game.getFileSystemManager();

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

FileSystemManager.appendFileSync(filePath, data, encoding)

同步将内容追加到文件末尾。接受 filePath(字符串)、data(字符串或 ArrayBuffer)、以及 encoding(字符串,非必填)作为位置参数。

注意:从 SDK 版本 0.18.0 开始支持。低版本需进行兼容性适配。

示例

const fs = TTMinis.game.getFileSystemManager();

try {
  fs.appendFileSync(
    `${TTMinis.game.env.USER_DATA_PATH}/hello.txt`,
    'some text',
    'utf8'
  )
  console.log('appendFile success')
} catch(e) {
  console.error(e)
}

FileSystemManager.truncate(opts)

截断文件内容。

注意:从 SDK 版本 0.18.0 开始支持。低版本需进行兼容性适配。

参数

字段

类型

描述

是否必填

filePath

string

要截断的文件路径(本地路径)

是

length

int32

截断位置。默认值:0。如果 length 小于文件字节大小,则仅保留前 length 个字节,其余部分将被移除。如果 length 大于文件大小,则文件将被扩展,扩展部分用空字节 ('\0') 填充。

否

success

function

API 调用成功时的回调函数

否

fail

function

API 调用失败时的回调函数

否

complete

function

API 调用结束时的回调函数(无论成功或失败均执行)

否

示例

const fs = TTMinis.game.getFileSystemManager();

fs.truncate({
  filePath: `${TTMinis.game.env.USER_DATA_PATH}/hello.txt`,
  length: 10,
  success(res) {
    console.log(res)
  },
  fail(res) {
    console.error(res)
  }
})

FileSystemManager.truncateSync(opts)

同步截断文件内容。接受包含 filePath(字符串,必填)和 length(int32,选填,默认值为 0)的参数对象。

注意:从 SDK 版本 0.18.0 开始支持。低版本需进行兼容性适配。

示例

const fs = TTMinis.game.getFileSystemManager();

try {
  fs.truncateSync({
    filePath: `${TTMinis.game.env.USER_DATA_PATH}/hello.txt`,
    length: 10
  })
  console.log('truncate success')
} catch(e) {
  console.error(e)
}

FileSystemManager.ftruncate(opts)

使用文件描述符截断文件内容。

注意:从 SDK 版本 0.18.0 开始支持。低版本需进行兼容性适配。

参数

字段

类型

描述

是否必填

fd

string

文件描述符。通过 FileSystemManager.open 或 FileSystemManager.openSync 获取。

是

length

int32

截断位置。默认值:0。如果 length 小于文件字节大小,则仅保留前 length 个字节,其余部分将被移除。如果 length 大于文件大小,则文件将被扩展,扩展部分用空字节 ('\0') 填充。

是

success

function

API 调用成功时的回调函数

否

fail

function

API 调用失败时的回调函数

否

complete

function

API 调用结束时的回调函数(无论成功或失败均执行)

否

示例

const fs = TTMinis.game.getFileSystemManager();

fs.open({
  filePath: `${TTMinis.game.env.USER_DATA_PATH}/hello.txt`,
  flag: 'a+',
  success(res) {
    fs.ftruncate({
      fd: res.fd,
      length: 10,
      success(res) {
        console.log(res)
      },
      fail(res) {
        console.error(res)
      }
    })
  }
})

FileSystemManager.ftruncateSync(opts)

使用文件描述符同步截断文件内容。接受包含 fd(字符串,必填)和 length(int32,必填)的参数对象。

注意:从 SDK 版本 0.18.0 开始支持。低版本需进行兼容性适配。

示例

const fs = TTMinis.game.getFileSystemManager();

try {
  const fd = fs.openSync({
    filePath: `${TTMinis.game.env.USER_DATA_PATH}/hello.txt`,
    flag: 'a+'
  })
  fs.ftruncateSync({
    fd: fd,
    length: 10
  })
  console.log('ftruncate success')
} catch(e) {
  console.error(e)
}

FileSystemManager.readCompressedFile(opts)

读取指定压缩类型的本地文件内容。

注意:从 SDK 版本 0.18.0 开始支持。低版本需进行兼容性适配。

参数

字段

类型

描述

是否必填

filePath

string

要读取的文件路径(本地用户文件或代码包文件)

是

compressionAlgorithm

string

文件压缩类型。当前仅支持 br(Brotli 压缩文件)。

是

success

function

API 调用成功时的回调函数

否

fail

function

API 调用失败时的回调函数

否

complete

function

API 调用结束时的回调函数(无论成功或失败均执行)

否

success 回调会收到包含以下字段的结果对象:

字段

类型

描述

data

ArrayBuffer

文件内容

示例

const fs = TTMinis.game.getFileSystemManager();

fs.readCompressedFile({
  filePath: `${TTMinis.game.env.USER_DATA_PATH}/hello.br`,
  compressionAlgorithm: 'br',
  success(res) {
    console.log(res.data)
  },
  fail(res) {
    console.error(res)
  }
})

FileSystemManager.readCompressedFileSync(opts)

同步读取指定压缩类型的本地文件内容。接受包含 filePath(字符串,必填)和 compressionAlgorithm(字符串,必填)的参数对象。

注意:从 SDK 版本 0.18.0 开始支持。低版本需进行兼容性适配。

返回值

ArrayBuffer - 文件内容。

示例

const fs = TTMinis.game.getFileSystemManager();

try {
  const data = fs.readCompressedFileSync({
    filePath: `${TTMinis.game.env.USER_DATA_PATH}/hello.br`,
    compressionAlgorithm: 'br'
  })
  console.log(data)
} catch(e) {
  console.error(e)
}

FileSystemManager.readZipEntry(opts)

读取压缩包内的文件。

注意:从 SDK 版本 0.18.0 开始支持。低版本需进行兼容性适配。

参数

字段

类型

描述

是否必填

filePath

string

要读取的压缩包路径(本地路径)

是

entries

list<object>|string

要从压缩包中读取的文件列表。传入 "all" 可读取压缩包中的所有文件。

是

encoding

string

指定读取文件时使用的字符编码。仅当 entries 为 "all" 时有效。如果 entries 为 "all" 且未提供 encoding,则文件内容以 ArrayBuffer 格式按二进制读取。

否

success

function

API 调用成功时的回调函数

否

fail

function

API 调用失败时的回调函数

否

complete

function

API 调用结束时的回调函数(无论成功或失败均执行)

否

当 entries 为数组时,每一项的结构如下:

字段

类型

描述

是否必填

path

string

压缩包内的文件路径

是

encoding

string

指定读取文件时使用的字符编码。如果不提供,则文件内容以二进制形式(ArrayBuffer 格式)读取。

否

position

int32

开始读取的位置。如果不指定,则从文件开头开始读取。有效范围:[0, fileLength - 1]。单位:字节。

否

length

int32

要读取的字节数。如果不指定,则读取至文件末尾。有效范围:[1, fileLength]。单位:字节。

否

success 回调会收到包含以下字段的结果对象:

字段

类型

描述

entries

object

文件读取结果。res.entries 是一个对象,其中键为文件路径,值为包含 data(文件内容)和 errMsg(错误信息)属性的 FileItem 对象。

示例

const fs = TTMinis.game.getFileSystemManager();

// Read specific files from a zip
fs.readZipEntry({
  filePath: `${TTMinis.game.env.USER_DATA_PATH}/example.zip`,
  entries: [{
    path: 'some_folder/my_file.txt',
    encoding: 'utf-8',
    position: 0,
    length: 10000
  }, {
    path: 'other_folder/other_file.txt'
  }],
  success(res) {
    console.log(res.entries)
  },
  fail(res) {
    console.error(res)
  }
})

// Read all files from a zip
fs.readZipEntry({
  filePath: `${TTMinis.game.env.USER_DATA_PATH}/example.zip`,
  entries: 'all',
  encoding: 'utf-8',
  success(res) {
    console.log(res.entries)
  },
  fail(res) {
    console.error(res)
  }
})

Stats

Stats.isDirectory()

检查当前文件是否为目录。

返回值

Boolean - 当前文件是否为目录。

Stats.isFile()

检查当前文件是否为常规文件。

返回值

Boolean - 当前文件是否为常规文件。

ReadResult

文件读取结果,由 FileSystemManager.readSync 接口返回。

字段

类型

描述

bytesRead

int32

实际读取的字节数

arrayBuffer

ArrayBuffer

被写入的缓冲区对象,即 arrayBuffer 输入参数

FileStats

每个 FileStats 对象包含 path 和 stats。

字段

类型

描述

path

string

文件或目录路径

stats

Stats

描述文件状态的 Stats 对象

TikTok for Developers