文件管理器
判断文件/目录是否存在
在文件结尾追加内容
保存临时文件到本地。此接口会移动临时文件,因此调用成功后,tempFilePath 将不可用。
获取该小程序下已保存的本地缓存文件列表
删除该小程序下已保存的本地缓存文件
复制文件
获取该小程序下的 本地临时文件 或 本地缓存文件 信息
创建目录
读取目录内文件列表
读取本地文件内容
重命名文件。可以把文件从 oldPath 移动到 newPath
删除目录
获取文件 Stats 对象
删除文件
解压文件
写文件
判断文件/目录是否存在
| 属性 | 类型 | 默认值 | 必填 | 说明 | 
|---|---|---|---|---|
| path | string | 是 | 要判断是否存在的文件/目录路径 | |
| success | function | 否 | 接口调用成功的回调函数 | |
| fail | function | 否 | 接口调用失败的回调函数 | |
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) | 
参数
Object res
| 属性 | 类型 | 说明 | 
|---|---|---|
| errMsg | string | 错误信息 | 
res.errMsg 的合法值
| 值 | 说明 | 
|---|---|
| fail no such file or directory ${path} | 文件/目录不存在 | 
FileSystemManager.access 的同步版本
要判断是否存在的文件/目录路径
| 错误码 | 错误信息 | 说明 | 
|---|
| fail no such file or directory ${path} | 文件/目录不存在
在文件结尾追加内容
| 属性 | 类型 | 默认值 | 必填 | 说明 | 
|---|---|---|---|---|
| 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 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 是一个已经存在的目录 | 
FileSystemManager.appendFile 的同步版本
要追加内容的文件路径
要追加的文本或二进制数据
指定写入文件的字符编码
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 是一个已经存在的目录
复制文件
| 属性 | 类型 | 默认值 | 必填 | 说明 | 
|---|---|---|---|---|
| srcPath | string | 是 | 源文件路径,只可以是普通文件 | |
| destPath | string | 是 | 目标文件路径 | |
| success | function | 否 | 接口调用成功的回调函数 | |
| fail | function | 否 | 接口调用失败的回调函数 | |
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) | 
参数
Object res
| 属性 | 类型 | 说明 | 
|---|---|---|
| errMsg | string | 错误信息 | 
res.errMsg 的合法值
| 值 | 说明 | 
|---|---|
| fail permission denied, copyFile ${srcPath} -> ${destPath} | 指定目标文件路径没有写权限 | 
| fail no such file or directory, copyFile ${srcPath} -> ${destPath} | 源文件不存在,或目标文件路径的上层目录不存在 | 
FileSystemManager.copyFile 的同步版本
源文件路径,只可以是普通文件
目标文件路径
| 错误码 | 错误信息 | 说明 | 
|---|
| fail permission denied, copyFile ${srcPath} -> ${destPath}         | 指定目标文件路径没有写权限         
| fail no such file or directory, copyFile ${srcPath} -> ${destPath} | 源文件不存在,或目标文件路径的上层目录不存在
获取该小程序下的 本地临时文件 或 本地缓存文件 信息
| 属性 | 类型 | 默认值 | 必填 | 说明 | 
|---|---|---|---|---|
| filePath | string | 是 | 要读取的文件路径 | |
| success | function | 否 | 接口调用成功的回调函数 | |
| fail | function | 否 | 接口调用失败的回调函数 | |
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) | 
参数
Object res
| 属性 | 类型 | 说明 | 
|---|---|---|
| size | number | 文件大小,以字节为单位 | 
参数
Object res
| 属性 | 类型 | 说明 | 
|---|---|---|
| errMsg | string | 错误信息 | 
res.errMsg 的合法值
| 值 | 说明 | 
|---|---|
| fail file not exist | 指定的 filePath 找不到文件 | 
获取该小程序下已保存的本地缓存文件列表
| 属性 | 类型 | 默认值 | 必填 | 说明 | 
|---|---|---|---|---|
| success | function | 否 | 接口调用成功的回调函数 | |
| fail | function | 否 | 接口调用失败的回调函数 | |
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) | 
参数
Object res
| 属性 | 类型 | 说明 | 
|---|---|---|
| fileList | Array.<Object> | 文件数组 | 
res.fileList 的结构
| 属性 | 类型 | 说明 | 
|---|---|---|
| filePath | string | 本地路径 | 
| size | number | 本地文件大小,以字节为单位 | 
| createTime | number | 文件保存时的时间戳,从1970/01/01 08:00:00 到当前时间的秒数 | 
创建目录
| 属性 | 类型 | 默认值 | 必填 | 说明 | 最低版本 | 
|---|---|---|---|---|---|
| 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 res
| 属性 | 类型 | 说明 | 
|---|---|---|
| errMsg | string | 错误信息 | 
res.errMsg 的合法值
| 值 | 说明 | 
|---|---|
| fail no such file or directory ${dirPath} | 上级目录不存在 | 
| fail permission denied, open ${dirPath} | 指定的 filePath 路径没有写权限 | 
| fail file already exists ${dirPath} | 有同名文件或目录 | 
FileSystemManager.mkdir 的同步版本
创建的目录路径
是否在递归创建该目录的上级目录后再创建该目录。如果对应的上级目录已经存在,则不创建该上级目录。如 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}       | 有同名文件或目录            
读取目录内文件列表
| 属性 | 类型 | 默认值 | 必填 | 说明 | 
|---|---|---|---|---|
| dirPath | string | 是 | 要读取的目录路径 | |
| success | function | 否 | 接口调用成功的回调函数 | |
| fail | function | 否 | 接口调用失败的回调函数 | |
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) | 
参数
Object res
| 属性 | 类型 | 说明 | 
|---|---|---|
| files | Array.<string> | 指定目录下的文件名数组。 | 
参数
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 路径没有读权限 | 
FileSystemManager.readdir 的同步版本
要读取的目录路径
指定目录下的文件名数组。
| 错误码 | 错误信息 | 说明 | 
|---|
| fail no such file or directory ${dirPath} | 目录不存在               
| fail not a directory ${dirPath}           | dirPath 不是目录        
| fail permission denied, open ${dirPath}   | 指定的 filePath 路径没有读权限
读取本地文件内容。单个文件大小上限为100M。
| 属性 | 类型 | 默认值 | 必填 | 说明 | 
|---|---|---|---|---|
| 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 res
| 属性 | 类型 | 说明 | 
|---|---|---|
| data | string/ArrayBuffer | 文件内容 | 
参数
Object res
| 属性 | 类型 | 说明 | 
|---|---|---|
| errMsg | string | 错误信息 | 
res.errMsg 的合法值
| 值 | 说明 | 
|---|---|
| fail no such file or directory, open ${filePath} | 指定的 filePath 所在目录不存在 | 
| fail permission denied, open ${dirPath} | 指定的 filePath 路径没有读权限 | 
FileSystemManager.readFile 的同步版本
要读取的文件的路径
指定读取文件的字符编码,如果不传 encoding,则以 ArrayBuffer 格式读取文件的二进制内容
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 路径没有读权限
删除该小程序下已保存的本地缓存文件
| 属性 | 类型 | 默认值 | 必填 | 说明 | 
|---|---|---|---|---|
| filePath | string | 是 | 需要删除的文件路径 | |
| success | function | 否 | 接口调用成功的回调函数 | |
| fail | function | 否 | 接口调用失败的回调函数 | |
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) | 
参数
Object res
| 属性 | 类型 | 说明 | 
|---|---|---|
| errMsg | string | 错误信息 | 
res.errMsg 的合法值
| 值 | 说明 | 
|---|---|
| fail file not exist | 指定的 tempFilePath 找不到文件 | 
重命名文件。可以把文件从 oldPath 移动到 newPath
| 属性 | 类型 | 默认值 | 必填 | 说明 | 
|---|---|---|---|---|
| oldPath | string | 是 | 源文件路径,可以是普通文件或目录 | |
| newPath | string | 是 | 新文件路径 | |
| success | function | 否 | 接口调用成功的回调函数 | |
| fail | function | 否 | 接口调用失败的回调函数 | |
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) | 
参数
Object res
| 属性 | 类型 | 说明 | 
|---|---|---|
| errMsg | string | 错误信息 | 
res.errMsg 的合法值
| 值 | 说明 | 
|---|---|
| fail permission denied, rename ${oldPath} -> ${newPath} | 指定源文件或目标文件没有写权限 | 
| fail no such file or directory, rename ${oldPath} -> ${newPath} | 源文件不存在,或目标文件路径的上层目录不存在 | 
FileSystemManager.rename 的同步版本
源文件路径,可以是普通文件或目录
新文件路径
| 错误码 | 错误信息 | 说明 | 
|---|
| fail permission denied, rename ${oldPath} -> ${newPath}         | 指定源文件或目标文件没有写权限       
| fail no such file or directory, rename ${oldPath} -> ${newPath} | 源文件不存在,或目标文件路径的上层目录不存在
删除目录
| 属性 | 类型 | 默认值 | 必填 | 说明 | 最低版本 | 
|---|---|---|---|---|---|
| dirPath | string | 是 | 要删除的目录路径 | ||
| recursive | boolean | false | 否 | 是否递归删除目录。如果为 true,则删除该目录和该目录下的所有子目录以及文件。 | |
| success | function | 否 | 接口调用成功的回调函数 | ||
| fail | function | 否 | 接口调用失败的回调函数 | ||
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) | 
参数
Object res
| 属性 | 类型 | 说明 | 
|---|---|---|
| errMsg | string | 错误信息 | 
res.errMsg 的合法值
| 值 | 说明 | 
|---|---|
| fail no such file or directory ${dirPath} | 目录不存在 | 
| fail directory not empty | 目录不为空 | 
| fail permission denied, open ${dirPath} | 指定的 dirPath 路径没有写权限 | 
FileSystemManager.rmdir 的同步版本
要删除的目录路径
是否递归删除目录。如果为 true,则删除该目录和该目录下的所有子目录以及文件。
| 错误码 | 错误信息 | 说明 | 
|---|
| fail no such file or directory ${dirPath} | 目录不存在              
| fail directory not empty                  | 目录不为空              
| fail permission denied, open ${dirPath}   | 指定的 dirPath 路径没有写权限
保存临时文件到本地。此接口会移动临时文件,因此调用成功后,tempFilePath 将不可用。
| 属性 | 类型 | 默认值 | 必填 | 说明 | 
|---|---|---|---|---|
| tempFilePath | string | 是 | 临时存储文件路径 | |
| filePath | string | 否 | 要存储的文件路径 | |
| success | function | 否 | 接口调用成功的回调函数 | |
| fail | function | 否 | 接口调用失败的回调函数 | |
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) | 
参数
Object res
| 属性 | 类型 | 说明 | 
|---|---|---|
| savedFilePath | number | 存储后的文件路径 | 
参数
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}" | 上级目录不存在 | 
FileSystemManager.saveFile 的同步版本
临时存储文件路径
要存储的文件路径
存储后的文件路径
| 错误码 | 错误信息 | 说明 | 
|---|
| fail tempFilePath file not exist            | 指定的 tempFilePath 找不到文件
| fail permission denied, open "${filePath}"  | 指定的 filePath 路径没有写权限  
| fail no such file or directory "${dirPath}" | 上级目录不存在               
获取文件 Stats 对象
| 属性 | 类型 | 默认值 | 必填 | 说明 | 最低版本 | 
|---|---|---|---|---|---|
| path | string | 是 | 文件/目录路径 | ||
| recursive | boolean | false | 否 | 是否递归获取目录下的每个文件的 Stats 信息 | |
| success | function | 否 | 接口调用成功的回调函数 | ||
| fail | function | 否 | 接口调用失败的回调函数 | ||
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) | 
参数
Object res
| 属性 | 类型 | 说明 | 
|---|---|---|
| stats | Stats/Object | 当 recursive 为 false 时,res.stats 是一个 Stats 对象。当 recursive 为 true 且 path 是一个目录的路径时,res.stats 是一个 Object,key 以 path 为根路径的相对路径,value 是该路径对应的 Stats 对象。 | 
参数
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())
  }
})
 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())
    })
  }
})
 FileSystemManager.stat 的同步版本
文件/目录路径
是否递归获取目录下的每个文件的 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())
  }
})
 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())
    })
  }
})
 删除文件
| 属性 | 类型 | 默认值 | 必填 | 说明 | 
|---|---|---|---|---|
| filePath | string | 是 | 要删除的文件路径 | |
| success | function | 否 | 接口调用成功的回调函数 | |
| fail | function | 否 | 接口调用失败的回调函数 | |
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) | 
参数
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 是一个目录 | 
FileSystemManager.unlink 的同步版本
要删除的文件路径
| 错误码 | 错误信息 | 说明 | 
|---|
| fail permission denied, open ${path}             | 指定的 path 路径没有读权限  
| fail no such file or directory ${path}           | 文件不存在             
| fail operation not permitted, unlink ${filePath} | 传入的 filePath 是一个目录
解压文件
| 属性 | 类型 | 默认值 | 必填 | 说明 | 
|---|---|---|---|---|
| zipFilePath | string | 是 | 源文件路径,只可以是 zip 压缩文件 | |
| targetPath | string | 是 | 目标目录路径 | |
| success | function | 否 | 接口调用成功的回调函数 | |
| fail | function | 否 | 接口调用失败的回调函数 | |
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) | 
参数
Object res
| 属性 | 类型 | 说明 | 
|---|---|---|
| errMsg | string | 错误信息 | 
res.errMsg 的合法值
| 值 | 说明 | 
|---|---|
| fail permission denied, unzip ${zipFilePath} -> ${destPath} | 指定目标文件路径没有写权限 | 
| fail no such file or directory, unzip ${zipFilePath} -> "${destPath} | 源文件不存在,或目标文件路径的上层目录不存在 | 
写文件
| 属性 | 类型 | 默认值 | 必填 | 说明 | 
|---|---|---|---|---|
| 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 res
| 属性 | 类型 | 说明 | 
|---|---|---|
| errMsg | string | 错误信息 | 
res.errMsg 的合法值
| 值 | 说明 | 
|---|---|
| fail no such file or directory, open ${filePath} | 指定的 filePath 所在目录不存在 | 
| fail permission denied, open ${dirPath} | 指定的 filePath 路径没有写权限 | 
FileSystemManager.writeFile 的同步版本
要写入的文件路径
要写入的文本或二进制数据
指定写入文件的字符编码
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 路径没有写权限
← qq.getFileInfo Stats →