# FileSystemManager

文件管理器

# 方法

# FileSystemManager.access(Object object)

判断文件/目录是否存在

# FileSystemManager.appendFile(Object object)

在文件结尾追加内容

# FileSystemManager.saveFile(Object object)

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

# FileSystemManager.getSavedFileList(Object object)

获取该小程序下已保存的本地缓存文件列表

# FileSystemManager.removeSavedFile(Object object)

删除该小程序下已保存的本地缓存文件

# FileSystemManager.copyFile(Object object)

复制文件

# FileSystemManager.getFileInfo(Object object)

获取该小程序下的 本地临时文件 或 本地缓存文件 信息

# FileSystemManager.mkdir(Object object)

创建目录

# FileSystemManager.readdir(Object object)

读取目录内文件列表

# FileSystemManager.readFile(Object object)

读取本地文件内容

# FileSystemManager.rename(Object object)

重命名文件。可以把文件从 oldPath 移动到 newPath

# FileSystemManager.rmdir(Object object)

删除目录

# FileSystemManager.stat(Object object)

获取文件 Stats 对象

删除文件

# FileSystemManager.unzip(Object object)

解压文件

# FileSystemManager.writeFile(Object object)

写文件

# .access

# FileSystemManager.access(Object object)

判断文件/目录是否存在

# 参数

# Object object
属性 类型 默认值 必填 说明
path string 是 要判断是否存在的文件/目录路径
success function 否 接口调用成功的回调函数
fail function 否 接口调用失败的回调函数
complete function 否 接口调用结束的回调函数(调用成功、失败都会执行)
# object.fail 回调函数

参数

Object res

属性 类型 说明
errMsg string 错误信息

res.errMsg 的合法值

值 说明
fail no such file or directory ${path} 文件/目录不存在

# .accessSync

# FileSystemManager.accessSync(string path)

FileSystemManager.access 的同步版本

# 参数

# string path

要判断是否存在的文件/目录路径

# 错误

错误码 错误信息 说明
| fail no such file or directory ${path} | 文件/目录不存在

# .appendFile

# FileSystemManager.appendFile(Object object)

在文件结尾追加内容

# 参数

# Object object
属性 类型 默认值 必填 说明
filePath string 是 要追加内容的文件路径
data string/ArrayBuffer 是 要追加的文本或二进制数据
encoding string utf8 否 指定写入文件的字符编码
success function 否 接口调用成功的回调函数
fail function 否 接口调用失败的回调函数
complete function 否 接口调用结束的回调函数(调用成功、失败都会执行)

object.encoding 的合法值

值 说明
ascii
base64
binary
hex
ucs2/ucs-2/utf16le/utf-16le 以小端序读取
utf-8/utf8
latin1
# object.fail 回调函数

参数

Object res

属性 类型 说明
errMsg string 错误信息

res.errMsg 的合法值

值 说明
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 sdcard not mounted 指定的 filePath 是一个已经存在的目录

# .appendFileSync

# FileSystemManager.appendFileSync(string filePath, string|ArrayBuffer data, string encoding)

FileSystemManager.appendFile 的同步版本

# 参数

# string filePath

要追加内容的文件路径

# string|ArrayBuffer data

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

# string encoding

指定写入文件的字符编码

encoding 的合法值

值 说明
ascii
base64
binary
hex
ucs2/ucs-2/utf16le/utf-16le 以小端序读取
utf-8/utf8
latin1

# 错误

错误码 错误信息 说明
| 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 sdcard not mounted                                   | 指定的 filePath 是一个已经存在的目录

# .copyFile

# FileSystemManager.copyFile(Object object)

复制文件

# 参数

# Object object
属性 类型 默认值 必填 说明
srcPath string 是 源文件路径,只可以是普通文件
destPath string 是 目标文件路径
success function 否 接口调用成功的回调函数
fail function 否 接口调用失败的回调函数
complete function 否 接口调用结束的回调函数(调用成功、失败都会执行)
# object.fail 回调函数

参数

Object res

属性 类型 说明
errMsg string 错误信息

res.errMsg 的合法值

值 说明
fail permission denied, copyFile ${srcPath} -> ${destPath} 指定目标文件路径没有写权限
fail no such file or directory, copyFile ${srcPath} -> ${destPath} 源文件不存在,或目标文件路径的上层目录不存在

# .copyFileSync

# FileSystemManager.copyFileSync(string srcPath, string destPath)

FileSystemManager.copyFile 的同步版本

# 参数

# string srcPath

源文件路径,只可以是普通文件

# string destPath

目标文件路径

# 错误

错误码 错误信息 说明
| fail permission denied, copyFile ${srcPath} -> ${destPath}         | 指定目标文件路径没有写权限         
| fail no such file or directory, copyFile ${srcPath} -> ${destPath} | 源文件不存在,或目标文件路径的上层目录不存在

# .getFileInfo

# FileSystemManager.getFileInfo(Object object)

获取该小程序下的 本地临时文件 或 本地缓存文件 信息

# 参数

# Object object
属性 类型 默认值 必填 说明
filePath string 是 要读取的文件路径
success function 否 接口调用成功的回调函数
fail function 否 接口调用失败的回调函数
complete function 否 接口调用结束的回调函数(调用成功、失败都会执行)
# object.success 回调函数

参数

Object res

属性 类型 说明
size number 文件大小,以字节为单位
# object.fail 回调函数

参数

Object res

属性 类型 说明
errMsg string 错误信息

res.errMsg 的合法值

值 说明
fail file not exist 指定的 filePath 找不到文件

# .getSavedFileList

# FileSystemManager.getSavedFileList(Object object)

获取该小程序下已保存的本地缓存文件列表

# 参数

# Object object
属性 类型 默认值 必填 说明
success function 否 接口调用成功的回调函数
fail function 否 接口调用失败的回调函数
complete function 否 接口调用结束的回调函数(调用成功、失败都会执行)
# object.success 回调函数

参数

Object res

属性 类型 说明
fileList Array.<Object> 文件数组

res.fileList 的结构

属性 类型 说明
filePath string 本地路径
size number 本地文件大小,以字节为单位
createTime number 文件保存时的时间戳,从1970/01/01 08:00:00 到当前时间的秒数

# .mkdir

# FileSystemManager.mkdir(Object object)

创建目录

# 参数

# Object object
属性 类型 默认值 必填 说明 最低版本
dirPath string 是 创建的目录路径
recursive boolean false 否 是否在递归创建该目录的上级目录后再创建该目录。如果对应的上级目录已经存在,则不创建该上级目录。如 dirPath 为 a/b/c/d 且 recursive 为 true,将创建 a 目录,再在 a 目录下创建 b 目录,以此类推直至创建 a/b/c 目录下的 d 目录。
success function 否 接口调用成功的回调函数
fail function 否 接口调用失败的回调函数
complete function 否 接口调用结束的回调函数(调用成功、失败都会执行)
# object.fail 回调函数

参数

Object res

属性 类型 说明
errMsg string 错误信息

res.errMsg 的合法值

值 说明
fail no such file or directory ${dirPath} 上级目录不存在
fail permission denied, open ${dirPath} 指定的 filePath 路径没有写权限
fail file already exists ${dirPath} 有同名文件或目录

# .mkdirSync

# FileSystemManager.mkdirSync(string dirPath, boolean recursive)

FileSystemManager.mkdir 的同步版本

# 参数

# string dirPath

创建的目录路径

# boolean recursive

是否在递归创建该目录的上级目录后再创建该目录。如果对应的上级目录已经存在,则不创建该上级目录。如 dirPath 为 a/b/c/d 且 recursive 为 true,将创建 a 目录,再在 a 目录下创建 b 目录,以此类推直至创建 a/b/c 目录下的 d 目录。

# 错误

错误码 错误信息 说明
| fail no such file or directory ${dirPath} | 上级目录不存在             
| fail permission denied, open ${dirPath}   | 指定的 filePath 路径没有写权限
| fail file already exists ${dirPath}       | 有同名文件或目录                       

# .readdir

# FileSystemManager.readdir(Object object)

读取目录内文件列表

# 参数

# Object object
属性 类型 默认值 必填 说明
dirPath string 是 要读取的目录路径
success function 否 接口调用成功的回调函数
fail function 否 接口调用失败的回调函数
complete function 否 接口调用结束的回调函数(调用成功、失败都会执行)
# object.success 回调函数

参数

Object res

属性 类型 说明
files Array.<string> 指定目录下的文件名数组。
# object.fail 回调函数

参数

Object res

属性 类型 说明
errMsg string 错误信息

res.errMsg 的合法值

值 说明
fail no such file or directory ${dirPath} 目录不存在
fail not a directory ${dirPath} dirPath 不是目录
fail permission denied, open ${dirPath} 指定的 filePath 路径没有读权限

# .readdirSync

# Array.<string> FileSystemManager.readdirSync(string dirPath)

FileSystemManager.readdir 的同步版本

# 参数

# string dirPath

要读取的目录路径

# 返回值

# Array.<string> files

指定目录下的文件名数组。

# 错误

错误码 错误信息 说明
| fail no such file or directory ${dirPath} | 目录不存在               
| fail not a directory ${dirPath}           | dirPath 不是目录        
| fail permission denied, open ${dirPath}   | 指定的 filePath 路径没有读权限   

# .readFile

# FileSystemManager.readFile(Object object)

读取本地文件内容。单个文件大小上限为100M。

# 参数

# Object object
属性 类型 默认值 必填 说明
filePath string 是 要读取的文件的路径
encoding string 否 指定读取文件的字符编码,如果不传 encoding,则以 ArrayBuffer 格式读取文件的二进制内容
success function 否 接口调用成功的回调函数
fail function 否 接口调用失败的回调函数
complete function 否 接口调用结束的回调函数(调用成功、失败都会执行)

object.encoding 的合法值

值 说明
ascii
base64
binary
hex
ucs2/ucs-2/utf16le/utf-16le 以小端序读取
utf-8/utf8
latin1
# object.success 回调函数

参数

Object res

属性 类型 说明
data string/ArrayBuffer 文件内容
# object.fail 回调函数

参数

Object res

属性 类型 说明
errMsg string 错误信息

res.errMsg 的合法值

值 说明
fail no such file or directory, open ${filePath} 指定的 filePath 所在目录不存在
fail permission denied, open ${dirPath} 指定的 filePath 路径没有读权限

# .readFileSync

# string|ArrayBuffer FileSystemManager.readFileSync(string filePath, string encoding)

FileSystemManager.readFile 的同步版本

# 参数

# string filePath

要读取的文件的路径

# string encoding

指定读取文件的字符编码,如果不传 encoding,则以 ArrayBuffer 格式读取文件的二进制内容

encoding 的合法值

值 说明
ascii
base64
binary
hex
ucs2/ucs-2/utf16le/utf-16le 以小端序读取
utf-8/utf8
latin1

# 返回值

# string|ArrayBuffer data

文件内容

# 错误

错误码 错误信息 说明
| fail no such file or directory, open ${filePath} | 指定的 filePath 所在目录不存在
| fail permission denied, open ${dirPath}          | 指定的 filePath 路径没有读权限

# .removeSavedFile

# FileSystemManager.removeSavedFile(Object object)

删除该小程序下已保存的本地缓存文件

# 参数

# Object object
属性 类型 默认值 必填 说明
filePath string 是 需要删除的文件路径
success function 否 接口调用成功的回调函数
fail function 否 接口调用失败的回调函数
complete function 否 接口调用结束的回调函数(调用成功、失败都会执行)
# object.fail 回调函数

参数

Object res

属性 类型 说明
errMsg string 错误信息

res.errMsg 的合法值

值 说明
fail file not exist 指定的 tempFilePath 找不到文件

# .rename

# FileSystemManager.rename(Object object)

重命名文件。可以把文件从 oldPath 移动到 newPath

# 参数

# Object object
属性 类型 默认值 必填 说明
oldPath string 是 源文件路径,可以是普通文件或目录
newPath string 是 新文件路径
success function 否 接口调用成功的回调函数
fail function 否 接口调用失败的回调函数
complete function 否 接口调用结束的回调函数(调用成功、失败都会执行)
# object.fail 回调函数

参数

Object res

属性 类型 说明
errMsg string 错误信息

res.errMsg 的合法值

值 说明
fail permission denied, rename ${oldPath} -> ${newPath} 指定源文件或目标文件没有写权限
fail no such file or directory, rename ${oldPath} -> ${newPath} 源文件不存在,或目标文件路径的上层目录不存在

# .renameSync

# FileSystemManager.renameSync(string oldPath, string newPath)

FileSystemManager.rename 的同步版本

# 参数

# string oldPath

源文件路径,可以是普通文件或目录

# string newPath

新文件路径

# 错误

错误码 错误信息 说明
| fail permission denied, rename ${oldPath} -> ${newPath}         | 指定源文件或目标文件没有写权限       
| fail no such file or directory, rename ${oldPath} -> ${newPath} | 源文件不存在,或目标文件路径的上层目录不存在

# .rmdir

# FileSystemManager.rmdir(Object object)

删除目录

# 参数

# Object object
属性 类型 默认值 必填 说明 最低版本
dirPath string 是 要删除的目录路径
recursive boolean false 否 是否递归删除目录。如果为 true,则删除该目录和该目录下的所有子目录以及文件。
success function 否 接口调用成功的回调函数
fail function 否 接口调用失败的回调函数
complete function 否 接口调用结束的回调函数(调用成功、失败都会执行)
# object.fail 回调函数

参数

Object res

属性 类型 说明
errMsg string 错误信息

res.errMsg 的合法值

值 说明
fail no such file or directory ${dirPath} 目录不存在
fail directory not empty 目录不为空
fail permission denied, open ${dirPath} 指定的 dirPath 路径没有写权限

# .rmdirSync

# FileSystemManager.rmdirSync(string dirPath, boolean recursive)

FileSystemManager.rmdir 的同步版本

# 参数

# string dirPath

要删除的目录路径

# boolean recursive

是否递归删除目录。如果为 true,则删除该目录和该目录下的所有子目录以及文件。

# 错误

错误码 错误信息 说明
| fail no such file or directory ${dirPath} | 目录不存在              
| fail directory not empty                  | 目录不为空              
| fail permission denied, open ${dirPath}   | 指定的 dirPath 路径没有写权限

# .saveFile

# FileSystemManager.saveFile(Object object)

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

# 参数

# Object object
属性 类型 默认值 必填 说明
tempFilePath string 是 临时存储文件路径
filePath string 否 要存储的文件路径
success function 否 接口调用成功的回调函数
fail function 否 接口调用失败的回调函数
complete function 否 接口调用结束的回调函数(调用成功、失败都会执行)
# object.success 回调函数

参数

Object res

属性 类型 说明
savedFilePath string 存储后的文件路径
# object.fail 回调函数

参数

Object res

属性 类型 说明
errMsg string 错误信息

res.errMsg 的合法值

值 说明
fail tempFilePath file not exist 指定的 tempFilePath 找不到文件
fail permission denied, open "${filePath}" 指定的 filePath 路径没有写权限
fail no such file or directory "${dirPath}" 上级目录不存在

# .saveFileSync

# number FileSystemManager.saveFileSync(string tempFilePath, string filePath)

FileSystemManager.saveFile 的同步版本

# 参数

# string tempFilePath

临时存储文件路径

# string filePath

要存储的文件路径

# 返回值

# number savedFilePath

存储后的文件路径

# 错误

错误码 错误信息 说明
| fail tempFilePath file not exist            | 指定的 tempFilePath 找不到文件
| fail permission denied, open "${filePath}"  | 指定的 filePath 路径没有写权限  
| fail no such file or directory "${dirPath}" | 上级目录不存在                             

# .stat

# FileSystemManager.stat(Object object)

获取文件 Stats 对象

# 参数

# Object object
属性 类型 默认值 必填 说明 最低版本
path string 是 文件/目录路径
recursive boolean false 否 是否递归获取目录下的每个文件的 Stats 信息
success function 否 接口调用成功的回调函数
fail function 否 接口调用失败的回调函数
complete function 否 接口调用结束的回调函数(调用成功、失败都会执行)
# object.success 回调函数

参数

Object res

属性 类型 说明
stats Stats/Object 当 recursive 为 false 时,res.stats 是一个 Stats 对象。当 recursive 为 true 且 path 是一个目录的路径时,res.stats 是一个 Object,key 以 path 为根路径的相对路径,value 是该路径对应的 Stats 对象。
# object.fail 回调函数

参数

Object res

属性 类型 说明
errMsg string 错误信息

res.errMsg 的合法值

值 说明
fail permission denied, open ${path} 指定的 path 路径没有读权限
fail no such file or directory ${path} 文件不存在

# 示例代码

recursive 为 false 时

const fs = qq.getFileSystemManager()
fs.stat({
  path: `${qq.env.USER_DATA_PATH}/testDir`,
  success: res => {
    console.log(res.stats.isDirectory())
  }
})
1
2
3
4
5
6
7

recursive 为 true 时

fs.stat({
  path: `${qq.env.USER_DATA_PATH}/testDir`,
  recursive: true,
  success: res => {
    Object.keys(res.stats).forEach(path => {
      const stats = res.stats[path]
      console.log(path, stats.isDirectory())
    })
  }
})
1
2
3
4
5
6
7
8
9
10

# .statSync

# Stats|Object FileSystemManager.statSync(string path, boolean recursive)

FileSystemManager.stat 的同步版本

# 参数

# string path

文件/目录路径

# boolean recursive

是否递归获取目录下的每个文件的 Stats 信息

# 返回值

# Stats|Object stats

当 recursive 为 false 时,res.stats 是一个 Stats 对象。当 recursive 为 true 且 path 是一个目录的路径时,res.stats 是一个 Object,key 以 path 为根路径的相对路径,value 是该路径对应的 Stats 对象。

# 错误

错误码 错误信息 说明
| fail permission denied, open ${path}   | 指定的 path 路径没有读权限
| fail no such file or directory ${path} | 文件不存在           

# 示例代码

recursive 为 false 时

const fs = qq.getFileSystemManager()
fs.stat({
  path: `${qq.env.USER_DATA_PATH}/testDir`,
  success: res => {
    console.log(res.stats.isDirectory())
  }
})
1
2
3
4
5
6
7

recursive 为 true 时

fs.stat({
  path: `${qq.env.USER_DATA_PATH}/testDir`,
  recursive: true,
  success: res => {
    Object.keys(res.stats).forEach(path => {
      const stats = res.stats[path]
      console.log(path, stats.isDirectory())
    })
  }
})
1
2
3
4
5
6
7
8
9
10

删除文件

# 参数

# Object object
属性 类型 默认值 必填 说明
filePath string 是 要删除的文件路径
success function 否 接口调用成功的回调函数
fail function 否 接口调用失败的回调函数
complete function 否 接口调用结束的回调函数(调用成功、失败都会执行)
# object.fail 回调函数

参数

Object res

属性 类型 说明
errMsg string 错误信息

res.errMsg 的合法值

值 说明
fail permission denied, open ${path} 指定的 path 路径没有读权限
fail no such file or directory ${path} 文件不存在
fail operation not permitted, unlink ${filePath} 传入的 filePath 是一个目录

# .unlinkSync

# FileSystemManager.unlinkSync(string filePath)

FileSystemManager.unlink 的同步版本

# 参数

# string filePath

要删除的文件路径

# 错误

错误码 错误信息 说明
| fail permission denied, open ${path}             | 指定的 path 路径没有读权限  
| fail no such file or directory ${path}           | 文件不存在             
| fail operation not permitted, unlink ${filePath} | 传入的 filePath 是一个目录

# .unzip

# FileSystemManager.unzip(Object object)

解压文件

# 参数

# Object object
属性 类型 默认值 必填 说明
zipFilePath string 是 源文件路径,只可以是 zip 压缩文件
targetPath string 是 目标目录路径
success function 否 接口调用成功的回调函数
fail function 否 接口调用失败的回调函数
complete function 否 接口调用结束的回调函数(调用成功、失败都会执行)
# object.fail 回调函数

参数

Object res

属性 类型 说明
errMsg string 错误信息

res.errMsg 的合法值

值 说明
fail permission denied, unzip ${zipFilePath} -> ${destPath} 指定目标文件路径没有写权限
fail no such file or directory, unzip ${zipFilePath} -> "${destPath} 源文件不存在,或目标文件路径的上层目录不存在

# .writeFile

# FileSystemManager.writeFile(Object object)

写文件

# 参数

# Object object
属性 类型 默认值 必填 说明
filePath string 是 要写入的文件路径
data string/ArrayBuffer 是 要写入的文本或二进制数据
encoding string utf8 否 指定写入文件的字符编码
success function 否 接口调用成功的回调函数
fail function 否 接口调用失败的回调函数
complete function 否 接口调用结束的回调函数(调用成功、失败都会执行)

object.encoding 的合法值

值 说明
ascii
base64
binary
hex
ucs2/ucs-2/utf16le/utf-16le 以小端序读取
utf-8/utf8
latin1
# object.fail 回调函数

参数

Object res

属性 类型 说明
errMsg string 错误信息

res.errMsg 的合法值

值 说明
fail no such file or directory, open ${filePath} 指定的 filePath 所在目录不存在
fail permission denied, open ${dirPath} 指定的 filePath 路径没有写权限

# .writeFileSync

# FileSystemManager.writeFileSync(string filePath, string|ArrayBuffer data, string encoding)

FileSystemManager.writeFile 的同步版本

# 参数

# string filePath

要写入的文件路径

# string|ArrayBuffer data

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

# string encoding

指定写入文件的字符编码

encoding 的合法值

值 说明
ascii
base64
binary
hex
ucs2/ucs-2/utf16le/utf-16le 以小端序读取
utf-8/utf8
latin1

# 错误

错误码 错误信息 说明
| fail no such file or directory, open ${filePath} | 指定的 filePath 所在目录不存在
| fail permission denied, open ${dirPath}          | 指定的 filePath 路径没有写权限