目录

FileSystemManager

优质
小牛编辑
120浏览
2023-12-01

FileSystemManager.access(Object object)

判断文件/目录是否存在

参数

Object object
属性类型必填说明
pathstring要判断是否存在的文件/目录路径 (本地路径)
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

object.fail 回调函数

参数
Object res
属性类型说明
errMsgstring错误信息

FileSystemManager.accessSync(string path)

FileSystemManager.access 的同步版本

参数

string path

要判断是否存在的文件/目录路径 (本地路径)

FileSystemManager.appendFile(Object object)

在文件结尾追加内容

参数

Object object
属性类型默认值必填说明
filePathstring要追加内容的文件路径 (本地路径)
datastring/ArrayBuffer要追加的文本或二进制数据
encodingstringutf8指定写入文件的字符编码
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

object.encoding 的合法值

说明
base64 

object.fail 回调函数

参数
Object res
属性类型说明
errMsgstring错误信息

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

FileSystemManager.appendFile 的同步版本

参数

string filePath

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

string|ArrayBuffer data

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

string encoding

指定写入文件的字符编码

encoding 的合法值

说明
base64 

FileSystemManager.copyFile(Object object)

复制文件

参数

Object object
属性类型必填说明
srcPathstring源文件路径,支持本地路径
destPathstring目标文件路径,支持本地路径
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

object.fail 回调函数

参数
Object res
属性类型说明
errMsgstring错误信息

FileSystemManager.copyFileSync(string srcPath, string destPath)

FileSystemManager.copyFile 的同步版本

参数

string srcPath

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

string destPath

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

FileSystemManager.getFileInfo(Object object)

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

参数

Object object
属性类型必填说明
filePathstring要读取的文件路径 (本地路径)
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

object.success 回调函数

参数
Object res
属性类型说明
sizenumber文件大小,以字节为单位

object.fail 回调函数

参数
Object res
属性类型说明
errMsgstring错误信息

FileSystemManager.getSavedFileList(Object object)

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

参数

Object object
属性类型必填说明
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

object.success 回调函数

参数
Object res
属性类型说明
fileListArray.<Object>文件数组

res.fileList 的结构

属性类型说明
filePathstring文件路径 (本地路径)
sizenumber本地文件大小,以字节为单位
createTimenumber文件保存时的时间戳,从1970/01/01 08:00:00 到当前时间的秒数

FileSystemManager.mkdir(Object object)

创建目录

参数

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

object.fail 回调函数

参数
Object res
属性类型说明
errMsgstring错误信息

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 目录。

FileSystemManager.readdir(Object object)

读取目录内文件列表

参数

Object object
属性类型必填说明
dirPathstring要读取的目录路径 (本地路径)
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

object.success 回调函数

参数
Object res
属性类型说明
filesArray.<string>指定目录下的文件名数组。

object.fail 回调函数

参数
Object res
属性类型说明
errMsgstring错误信息

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

FileSystemManager.readdir 的同步版本

参数

string dirPath

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

返回值

Array. files

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

FileSystemManager.readFile(Object object)

读取本地文件内容

参数

Object object
属性类型默认值必填说明
filePathstring要读取的文件的路径 (本地路径)
encodingstring指定读取文件的字符编码,如果不传 encoding,则以 ArrayBuffer 格式读取文件的二进制内容
positionstring从文件指定位置开始读,如果不指定,则从文件头开始读。读取的范围应该是左闭右开区间 [position, position+length)。有效范围:[0, fileLength - 1]。单位:byte
lengthstring指定文件的长度,如果不指定,则读到文件末尾。有效范围:[1, fileLength]。单位:byte
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

object.encoding 的合法值

说明
base64 

object.success 回调函数

参数
Object res
属性类型说明
datastring/ArrayBuffer文件内容

object.fail 回调函数

参数
Object res
属性类型说明
errMsgstring错误信息

string|ArrayBuffer FileSystemManager.readFileSync(string filePath, string encoding, string position, string length)

FileSystemManager.readFile 的同步版本

参数

string filePath

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

string encoding

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

encoding 的合法值

说明
base64 
string position

从文件指定位置开始读,如果不指定,则从文件头开始读。读取的范围应该是左闭右开区间 [position, position+length)。有效范围:[0, fileLength - 1]。单位:byte

string length

指定文件的长度,如果不指定,则读到文件末尾。有效范围:[1, fileLength]。单位:byte

返回值

string|ArrayBuffer data

文件内容

FileSystemManager.removeSavedFile(Object object)

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

参数

Object object
属性类型必填说明
filePathstring需要删除的文件路径 (本地路径)
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

object.fail 回调函数

参数
Object res
属性类型说明
errMsgstring错误信息

FileSystemManager.rename(Object object)

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

参数

Object object
属性类型必填说明
oldPathstring源文件路径,支持本地路径
newPathstring新文件路径,支持本地路径
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

object.fail 回调函数

参数
Object res
属性类型说明
errMsgstring错误信息

FileSystemManager.renameSync(string oldPath, string newPath)

FileSystemManager.rename 的同步版本

参数

string oldPath

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

string newPath

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

FileSystemManager.rmdir(Object object)

删除目录

参数

Object object
属性类型默认值必填说明
dirPathstring要删除的目录路径 (本地路径)
recursivebooleanfalse是否递归删除目录。如果为 true,则删除该目录和该目录下的所有子目录以及文件。
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

object.fail 回调函数

参数
Object res
属性类型说明
errMsgstring错误信息

FileSystemManager.rmdirSync(string dirPath, boolean recursive)

FileSystemManager.rmdir 的同步版本

参数

string dirPath

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

boolean recursive

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

FileSystemManager.saveFile(Object object)

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

参数

Object object
属性类型必填说明
tempFilePathstring临时存储文件路径 (本地路径)
filePathstring要存储的文件路径 (本地路径)
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

object.success 回调函数

参数
Object res
属性类型说明
savedFilePathstring存储后的文件路径 (本地路径)

object.fail 回调函数

参数
Object res
属性类型说明
errMsgstring错误信息

string FileSystemManager.saveFileSync(string tempFilePath, string filePath)

FileSystemManager.saveFile 的同步版本

参数

string tempFilePath

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

string filePath

要存储的文件路径 (本地路径)

返回值

string savedFilePath

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

FileSystemManager.stat(Object object)

获取文件 Stats 对象

参数

Object object
属性类型默认值必填说明
pathstring文件/目录路径 (本地路径)
recursivebooleanfalse是否递归获取目录下的每个文件的 Stats 信息
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

object.success 回调函数

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

object.fail 回调函数

参数
Object res
属性类型说明
errMsgstring错误信息

示例代码

//recursive 为 false 时
let fs = jd.getFileSystemManager()
fs.stat({
  path: `${jd.env.USER_DATA_PATH}/testDir`,
  success: res => {
    console.log(res.stats.isDirectory())
  }
})
//recursive 为 true 时
fs.stat({
  path: `${jd.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())
    })
  }
})

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 对象。

FileSystemManager.unlink(Object object)

删除文件

参数

Object object
属性类型必填说明
filePathstring要删除的文件路径 (本地路径)
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

object.fail 回调函数

参数
Object res
属性类型说明
errMsgstring错误信息

FileSystemManager.unlinkSync(string filePath)

FileSystemManager.unlink 的同步版本

参数

string filePath

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

FileSystemManager.unzip(Object object)

解压文件

参数

Object object
属性类型必填说明
zipFilePathstring源文件路径,支持本地路径, 只可以是 zip 压缩文件
targetPathstring目标目录路径, 支持本地路径
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

object.fail 回调函数

参数
Object res
属性类型说明
errMsgstring错误信息

FileSystemManager.writeFile(Object object)

写文件

参数

Object object
属性类型默认值必填说明
filePathstring要写入的文件路径 (本地路径)
datastring/ArrayBuffer要写入的文本或二进制数据
encodingstringutf8指定写入文件的字符编码
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

object.encoding 的合法值

说明
base64 

object.fail 回调函数

参数
Object res
属性类型说明
errMsgstring错误信息

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

FileSystemManager.writeFile 的同步版本

参数

string filePath

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

string|ArrayBuffer data

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

string encoding

指定写入文件的字符编码

encoding 的合法值

说明
base64