TikTok for Developers

Developer Docs

Last updated October 1, 2026

File

Grants access to a persistent file system manager for reading, writing, copying, and managing local files and directories within the mini game sandbox storage.

TTMinis.game.getFileSystemManager()

Gets the globally unique file system manager.

Returns

FileSystemManager - The file system manager instance.

Example

const fs = TTMinis.game.getFileSystemManager();

FileSystemManager

FileSystemManager.readFile(opts)

Reads local file content.

Parameters

Field

Type

Description

Required

filePath

string

The path of the file to read (local path)

Yes

encoding

string

Specifies the character encoding for reading the file. If encoding is not provided, the file content is read as binary in ArrayBuffer format.

No

position

int32

The position to start reading from. If not specified, reading starts from the beginning of the file. The valid range is [0, fileLength - 1]. Unit: byte.

No

length

int32

The number of bytes to read. If not specified, reads to the end of the file. Valid range: [1, fileLength]. Unit: byte.

No

success

function

The callback function for a successful API call

No

fail

function

The callback function for a failed API call

No

complete

function

The callback function for the end of the API call (executed for both success and failure)

No

The success callback receives a result object with the following fields:

Field

Type

Description

data

string | ArrayBuffer

The file content. Returns string or ArrayBuffer type.

errMsg

string

The error message. Returns "readFile:ok" on success.

Example

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)

Synchronously reads local file content. Accepts the same parameters as readFile but as positional arguments: filePath (string), encoding (string, optional), position (int32, optional), and length (int32, optional).

Returns

string | ArrayBuffer - The file content.

Example

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)

Checks whether a file or directory exists.

Parameters

Field

Type

Description

Required

path

string

The path of the file or directory to check (local path)

Yes

success

function

The callback function for a successful API call

No

fail

function

The callback function for a failed API call

No

complete

function

The callback function for the end of the API call (executed for both success and failure)

No

Example

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)

Synchronously checks whether a file or directory exists. Throws an exception if the path does not exist.

Parameters

Field

Type

Description

Required

path

string

The path of the file or directory to check (local path)

Yes

Example

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)

Writes data to a file.

Parameters

Field

Type

Description

Required

filePath

string

The path of the file to write (local path)

Yes

data

string | ArrayBuffer

The text or binary data to write

Yes

encoding

string

Specifies the character encoding for writing the file. Default: utf8.

No

success

function

The callback function for a successful API call

No

fail

function

The callback function for a failed API call

No

complete

function

The callback function for the end of the API call (executed for both success and failure)

No

Example

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)

Synchronously writes data to a file. Accepts the same parameters as writeFile but as positional arguments: filePath (string), data (string or ArrayBuffer), and encoding (string, optional).

Example

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)

Creates a directory.

Parameters

Field

Type

Description

Required

dirPath

string

The directory path to create (local path)

Yes

recursive

Boolean

If true, recursively creates parent directories before creating the target directory. If the parent directory already exists, it is not recreated. Default: false.

No

success

function

The callback function for a successful API call

No

fail

function

The callback function for a failed API call

No

complete

function

The callback function for the end of the API call (executed for both success and failure)

No

Example

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)

Synchronously creates a directory. Accepts dirPath (string) and recursive (Boolean, optional) as positional arguments.

Example

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)

Copies a file.

Parameters

Field

Type

Description

Required

srcPath

string

The source file path. Supports local paths.

Yes

destPath

string

The destination file path. Supports local paths.

Yes

success

function

The callback function for a successful API call

No

fail

function

The callback function for a failed API call

No

complete

function

The callback function for the end of the API call (executed for both success and failure)

No

Example

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)

Synchronously copies a file. Accepts srcPath (string) and destPath (string) as positional arguments.

Example

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)

Decompresses a file.

Parameters

Field

Type

Description

Required

zipFilePath

string

The source file path. Supports local paths. Must be a zip archive.

Yes

targetPath

string

The target directory path. Supports local paths.

Yes

success

function

The callback function for a successful API call

No

fail

function

The callback function for a failed API call

No

complete

function

The callback function for the end of the API call (executed for both success and failure)

No

FileSystemManager.open(opts)

Opens a file and returns a file descriptor.

Note: Supported starting from SDK version 0.15.0. Lower versions must be adjusted for compatibility.

Parameters

Field

Type

Description

Required

filePath

string

The file path (local path)

Yes

flag

string

The file system flag. Default: r.

No

success

function

The callback function for a successful API call

No

fail

function

The callback function for a failed API call

No

complete

function

The callback function for the end of the API call (executed for both success and failure)

No

Valid values for flag:

Value

Description

a

Opens the file for appending. Creates the file if it does not exist.

ax

Similar to a, but fails if the path exists

a+

Opens the file for reading and appending. Creates the file if it does not exist.

ax+

Similar to a+, but fails if the path exists

as

Opens the file for appending in synchronous mode. Creates the file if it does not exist.

as+

Opens the file for reading and appending in synchronous mode. Creates the file if it does not exist.

r

Opens the file for reading. Throws an exception if the file does not exist.

r+

Opens the file for reading and writing. Throws an exception if the file does not exist.

w

Opens the file for writing. Creates the file if it does not exist. Truncates the file if it exists.

wx

Similar to w, but fails if the path exists

w+

Opens the file for reading and writing. Creates the file if it does not exist. Truncates the file if it exists.

wx+

Similar to w+, but fails if the path exists

The success callback receives a result object with the following field:

Field

Type

Description

fd

string

The file descriptor

Example

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)

Synchronously opens a file and returns a file descriptor. Accepts the same parameters as open with filePath (string, required) and flag (string, optional, default r).

Note: Supported starting from SDK version 0.15.0. Lower versions must be adjusted for compatibility.

Returns

string - The file descriptor.

Example

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)

Closes a file.

Note: Supported starting from SDK version 0.15.0. Lower versions must be adjusted for compatibility.

Parameters

Field

Type

Description

Required

fd

string

The file descriptor to close. Obtained via FileSystemManager.open or FileSystemManager.openSync.

Yes

success

function

The callback function for a successful API call

No

fail

function

The callback function for a failed API call

No

complete

function

The callback function for the end of the API call (executed for both success and failure)

No

Example

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)

Synchronously closes a file. Accepts fd (string, required) as a parameter object.

Note: Supported starting from SDK version 0.15.0. Lower versions must be adjusted for compatibility.

Example

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)

Reads file content using a file descriptor.

Note: Supported starting from SDK version 0.15.0. Lower versions must be adjusted for compatibility.

Parameters

Field

Type

Description

Required

fd

string

The file descriptor. Obtained via FileSystemManager.open or FileSystemManager.openSync.

Yes

arrayBuffer

ArrayBuffer

The buffer to write data into. Must be an ArrayBuffer instance.

Yes

offset

int32

The write offset in the buffer. Default: 0.

No

length

int32

The number of bytes to read from the file. Default: 0.

No

position

int32

The starting position for reading. If not provided or null, reads from the current file pointer position. If a positive integer, the file pointer position remains unchanged and reading starts from position.

No

success

function

The callback function for a successful API call

No

fail

function

The callback function for a failed API call

No

complete

function

The callback function for the end of the API call (executed for both success and failure)

No

The success callback receives a result object with the following fields:

Field

Type

Description

bytesRead

int32

The actual number of bytes read

arrayBuffer

ArrayBuffer

The buffer object that was written to, which is the arrayBuffer input parameter

Example

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)

Synchronously reads file content using a file descriptor. Accepts the same parameters as read (without success, fail, and complete callbacks).

Note: Supported starting from SDK version 0.15.0. Lower versions must be adjusted for compatibility.

Returns

ReadResult object with the following fields:

Field

Type

Description

bytesRead

int32

The actual number of bytes read

arrayBuffer

ArrayBuffer

The buffer object that was written to, which is the arrayBuffer input parameter

FileSystemManager.write(opts)

Writes data to a file using a file descriptor.

Note: Supported starting from SDK version 0.15.0. Lower versions must be adjusted for compatibility.

Parameters

Field

Type

Description

Required

fd

string

The file descriptor. Obtained via FileSystemManager.open or FileSystemManager.openSync.

Yes

data

string | ArrayBuffer

The content to write. Type is string or ArrayBuffer.

Yes

offset

int32

Only effective when data is an ArrayBuffer. Determines the offset within the ArrayBuffer for the data to write. Default: 0.

No

length

int32

Only effective when data is an ArrayBuffer. Specifies the number of bytes to write. Default: the remaining bytes in the ArrayBuffer after offset.

No

encoding

string

Only effective when data is a string. Specifies the character encoding for writing. Default: utf8.

No

position

int32

Specifies the offset from the beginning of the file where data is written. If not provided or not a number, data is written at the current file pointer position.

No

success

function

The callback function for a successful API call

No

fail

function

The callback function for a failed API call

No

complete

function

The callback function for the end of the API call (executed for both success and failure)

No

The success callback receives a result object with the following field:

Field

Type

Description

bytesWritten

int32

The actual number of bytes written to the file. Note that the number of bytes written may not equal the number of characters in the string.

Example

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)

Synchronously writes data to a file using a file descriptor. Accepts the same parameters as write (without success, fail, and complete callbacks).

Note: Supported starting from SDK version 0.15.0. Lower versions must be adjusted for compatibility.

Returns

WriteResult object with the following field:

Field

Type

Description

bytesWritten

int32

The actual number of bytes written to the file

Example

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)

Removes a directory.

Note: Supported starting from SDK version 0.15.0. Lower versions must be adjusted for compatibility.

Parameters

Field

Type

Description

Required

dirPath

string

The directory path to remove (local path)

Yes

recursive

Boolean

If true, recursively removes the directory and all subdirectories and files within it. Default: false.

No

success

function

The callback function for a successful API call

No

fail

function

The callback function for a failed API call

No

complete

function

The callback function for the end of the API call (executed for both success and failure)

No

Example

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)

Synchronously removes a directory. Accepts dirPath (string) and recursive (Boolean, optional) as positional arguments.

Note: Supported starting from SDK version 0.15.0. Lower versions must be adjusted for compatibility.

Example

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)

Reads the file list within a directory.

Note: Supported starting from SDK version 0.15.0. Lower versions must be adjusted for compatibility.

Parameters

Field

Type

Description

Required

dirPath

string

The directory path to read (local path)

Yes

success

function

The callback function for a successful API call

No

fail

function

The callback function for a failed API call

No

complete

function

The callback function for the end of the API call (executed for both success and failure)

No

The success callback receives a result object with the following field:

Field

Type

Description

files

list<string>

An array of file names in the specified directory

Example

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)

Synchronously reads the file list within a directory. Accepts dirPath (string) as a positional argument.

Note: Supported starting from SDK version 0.15.0. Lower versions must be adjusted for compatibility.

Returns

list<string> - An array of file names in the specified directory.

Example

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)

Deletes a file.

Note: Supported starting from SDK version 0.15.0. Lower versions must be adjusted for compatibility.

Parameters

Field

Type

Description

Required

filePath

string

The file path to delete (local path)

Yes

success

function

The callback function for a successful API call

No

fail

function

The callback function for a failed API call

No

complete

function

The callback function for the end of the API call (executed for both success and failure)

No

Example

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)

Synchronously deletes a file. Accepts filePath (string) as a positional argument.

Note: Supported starting from SDK version 0.15.0. Lower versions must be adjusted for compatibility.

Example

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)

Gets the file Stats object.

Note: Supported starting from SDK version 0.16.0. Lower versions must be adjusted for compatibility.

Parameters

Field

Type

Description

Required

path

string

The file or directory path (local path)

Yes

recursive

Boolean

If true, recursively gets the Stats information for each file in the directory. Default: false.

No

success

function

The callback function for a successful API call

No

fail

function

The callback function for a failed API call

No

complete

function

The callback function for the end of the API call (executed for both success and failure)

No

The success callback receives a result object with the following field:

Field

Type

Description

stats

Stats | list<FileStats>

When recursive is false, stats is a Stats object. When recursive is true and path is a directory, stats is an array of FileStats objects, each containing path and stats.

Stats object

Field

Type

Description

mode

string

The file type and access permissions. Corresponds to POSIX stat.st_mode.

size

int32

The file size in bytes. Corresponds to POSIX stat.st_size.

lastAccessedTime

int32

The last time the file was accessed or executed. UNIX timestamp. Corresponds to POSIX stat.st_atime.

lastModifiedTime

int32

The last time the file was modified. UNIX timestamp. Corresponds to POSIX stat.st_mtime.

Stats object methods:

Method

Return type

Description

isDirectory()

Boolean

Checks whether the file is a directory

isFile()

Boolean

Checks whether the file is a regular file

Example

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)

Synchronously gets the file Stats object. Accepts path (string) and recursive (Boolean, optional) as positional arguments.

Note: Supported starting from SDK version 0.16.0. Lower versions must be adjusted for compatibility.

Returns

Stats | list<FileStats> - When recursive is false, returns a Stats object. When recursive is true and path is a directory, returns an array.

Example

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)

Gets the file status information using a file descriptor.

Note: Supported starting from SDK version 0.16.0. Lower versions must be adjusted for compatibility.

Parameters

Field

Type

Description

Required

fd

string

The file descriptor. Obtained via FileSystemManager.open or FileSystemManager.openSync.

Yes

success

function

The callback function for a successful API call

No

fail

function

The callback function for a failed API call

No

complete

function

The callback function for the end of the API call (executed for both success and failure)

No

The success callback receives a result object with the following field:

Field

Type

Description

stats

Stats

The Stats object containing the file status information

Example

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)

Synchronously gets the file status information using a file descriptor. Accepts a parameter object with fd (string, required).

Note: Supported starting from SDK version 0.16.0. Lower versions must be adjusted for compatibility.

Returns

Stats - The Stats object containing the file status information.

Example

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)

Renames a file. Moves a file from oldPath to newPath.

Note: Supported starting from SDK version 0.16.0. Lower versions must be adjusted for compatibility.

Parameters

Field

Type

Description

Required

oldPath

string

The source file path. Supports local paths.

Yes

newPath

string

The new file path. Supports local paths.

Yes

success

function

The callback function for a successful API call

No

fail

function

The callback function for a failed API call

No

complete

function

The callback function for the end of the API call (executed for both success and failure)

No

Example

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)

Synchronously renames a file. Accepts oldPath (string) and newPath (string) as positional arguments.

Note: Supported starting from SDK version 0.16.0. Lower versions must be adjusted for compatibility.

Example

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)

Gets information about a local temporary file or local cached file.

Note: Supported starting from SDK version 0.16.0. Lower versions must be adjusted for compatibility.

Parameters

Field

Type

Description

Required

filePath

string

The file path to read (local path)

Yes

digestAlgorithm

string

The algorithm for computing the file digest. Default: md5. Valid values: md5, sha1, sha256.

No

success

function

The callback function for a successful API call

No

fail

function

The callback function for a failed API call

No

complete

function

The callback function for the end of the API call (executed for both success and failure)

No

The success callback receives a result object with the following fields:

Field

Type

Description

size

int32

The file size in bytes

digest

string

The file digest computed using the specified digestAlgorithm

Example

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)

Saves a temporary file to local storage. This interface moves the temporary file, so tempFilePath is no longer available after a successful call.

Note: Supported starting from SDK version 0.16.0. Lower versions must be adjusted for compatibility.

Parameters

Field

Type

Description

Required

tempFilePath

string

The temporary file path (local path)

Yes

filePath

string

The destination file path (local path)

No

success

function

The callback function for a successful API call

No

fail

function

The callback function for a failed API call

No

complete

function

The callback function for the end of the API call (executed for both success and failure)

No

The success callback receives a result object with the following field:

Field

Type

Description

savedFilePath

string

The saved file path (local path)

Example

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)

Synchronously saves a temporary file to local storage. Accepts tempFilePath (string) and filePath (string, optional) as positional arguments.

Note: Supported starting from SDK version 0.16.0. Lower versions must be adjusted for compatibility.

Returns

string - The saved file path (local path).

Example

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)

Gets the list of saved local cache files.

Note: Supported starting from SDK version 0.16.0. Lower versions must be adjusted for compatibility.

Parameters

Field

Type

Description

Required

success

function

The callback function for a successful API call

No

fail

function

The callback function for a failed API call

No

complete

function

The callback function for the end of the API call (executed for both success and failure)

No

The success callback receives a result object with the following field:

Field

Type

Description

fileList

list<object>

An array of file objects

Each item in fileList has the following structure:

Field

Type

Description

filePath

string

The file path (local path)

size

int32

The local file size in bytes

createTime

int32

The timestamp when the file was saved, in seconds since 1970/01/01 08:00:00

Example

const fs = TTMinis.game.getFileSystemManager();

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

FileSystemManager.removeSavedFile(opts)

Removes a saved local cache file.

Note: Supported starting from SDK version 0.16.0. Lower versions must be adjusted for compatibility.

Parameters

Field

Type

Description

Required

filePath

string

The file path to remove (local path)

Yes

success

function

The callback function for a successful API call

No

fail

function

The callback function for a failed API call

No

complete

function

The callback function for the end of the API call (executed for both success and failure)

No

Example

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)

Appends content to the end of a file.

Note: Supported starting from SDK version 0.18.0. Lower versions must be adjusted for compatibility.

Parameters

Field

Type

Description

Required

filePath

string

The file path to append to (local path)

Yes

data

string | ArrayBuffer

The text or binary data to append

Yes

encoding

string

Specifies the character encoding for writing. Default: utf8.

No

success

function

The callback function for a successful API call

No

fail

function

The callback function for a failed API call

No

complete

function

The callback function for the end of the API call (executed for both success and failure)

No

Example

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)

Synchronously appends content to the end of a file. Accepts filePath (string), data (string or ArrayBuffer), and encoding (string, optional) as positional arguments.

Note: Supported starting from SDK version 0.18.0. Lower versions must be adjusted for compatibility.

Example

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)

Truncates the content of a file.

Note: Supported starting from SDK version 0.18.0. Lower versions must be adjusted for compatibility.

Parameters

Field

Type

Description

Required

filePath

string

The file path to truncate (local path)

Yes

length

int32

The truncation position. Default: 0. If length is less than the file size in bytes, only the first length bytes are retained and the rest is removed. If length is greater than the file size, the file is extended and the extended portion is filled with null bytes ('\0').

No

success

function

The callback function for a successful API call

No

fail

function

The callback function for a failed API call

No

complete

function

The callback function for the end of the API call (executed for both success and failure)

No

Example

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)

Synchronously truncates the content of a file. Accepts a parameter object with filePath (string, required) and length (int32, optional, default 0).

Note: Supported starting from SDK version 0.18.0. Lower versions must be adjusted for compatibility.

Example

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)

Truncates the content of a file using a file descriptor.

Note: Supported starting from SDK version 0.18.0. Lower versions must be adjusted for compatibility.

Parameters

Field

Type

Description

Required

fd

string

The file descriptor. Obtained via FileSystemManager.open or FileSystemManager.openSync.

Yes

length

int32

The truncation position. Default: 0. If length is less than the file size in bytes, only the first length bytes are retained and the rest is removed. If length is greater than the file size, the file is extended and the extended portion is filled with null bytes ('\0').

Yes

success

function

The callback function for a successful API call

No

fail

function

The callback function for a failed API call

No

complete

function

The callback function for the end of the API call (executed for both success and failure)

No

Example

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)

Synchronously truncates the content of a file using a file descriptor. Accepts a parameter object with fd (string, required) and length (int32, required).

Note: Supported starting from SDK version 0.18.0. Lower versions must be adjusted for compatibility.

Example

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)

Reads the content of a local file with a specified compression type.

Note: Supported starting from SDK version 0.18.0. Lower versions must be adjusted for compatibility.

Parameters

Field

Type

Description

Required

filePath

string

The file path to read (local user file or code package file)

Yes

compressionAlgorithm

string

The file compression type. Currently only supports br (brotli compressed file).

Yes

success

function

The callback function for a successful API call

No

fail

function

The callback function for a failed API call

No

complete

function

The callback function for the end of the API call (executed for both success and failure)

No

The success callback receives a result object with the following field:

Field

Type

Description

data

ArrayBuffer

The file content

Example

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)

Synchronously reads the content of a local file with a specified compression type. Accepts a parameter object with filePath (string, required) and compressionAlgorithm (string, required).

Note: Supported starting from SDK version 0.18.0. Lower versions must be adjusted for compatibility.

Returns

ArrayBuffer - The file content.

Example

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)

Reads files within a compressed archive.

Note: Supported starting from SDK version 0.18.0. Lower versions must be adjusted for compatibility.

Parameters

Field

Type

Description

Required

filePath

string

The path of the compressed archive to read (local path)

Yes

entries

list<object> | string

The list of files to read from the archive. Pass "all" to read all files in the archive.

Yes

encoding

string

Specifies the character encoding for reading files. Only effective when entries is "all". If entries is "all" and encoding is not provided, the file content is read as binary in ArrayBuffer format.

No

success

function

The callback function for a successful API call

No

fail

function

The callback function for a failed API call

No

complete

function

The callback function for the end of the API call (executed for both success and failure)

No

When entries is an array, each item has the following structure:

Field

Type

Description

Required

path

string

The file path within the archive

Yes

encoding

string

Specifies the character encoding for reading the file. If not provided, the file content is read as binary in ArrayBuffer format.

No

position

int32

The position to start reading from. If not specified, reading starts from the beginning of the file. Valid range: [0, fileLength - 1]. Unit: byte.

No

length

int32

The number of bytes to read. If not specified, reads to the end of the file. Valid range: [1, fileLength]. Unit: byte.

No

The success callback receives a result object with the following field:

Field

Type

Description

entries

object

The file reading results. res.entries is an object where keys are file paths and values are FileItem objects containing data (file content) and errMsg (error message) properties.

Example

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()

Checks whether the current file is a directory.

Returns

Boolean - Whether the current file is a directory.

Stats.isFile()

Checks whether the current file is a regular file.

Returns

Boolean - Whether the current file is a regular file.

ReadResult

The file reading result, returned by the FileSystemManager.readSync interface.

Field

Type

Description

bytesRead

int32

The actual number of bytes read

arrayBuffer

ArrayBuffer

The buffer object that was written to, which is the arrayBuffer input parameter

FileStats

Each FileStats object contains path and stats.

Field

Type

Description

path

string

The file or directory path

stats

Stats

The Stats object describing the file status

TikTok for Developers