FileSystemManager qg.getFileSystemManager()

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

返回值

FileSystemManager 文件管理器对象


示例代码:

var fs = qg.getFileSystemManager()

FileSystemManager.access(object)

判断文件/目录是否存在

参数

object

属性 类型 是否必填 说明
path string 要判断是否存在的文件/目录路径
success function 接口调用成功的回调函数
fail function 接口调用失败的回调函数
complete function 接口调用结束的回调函数

FileSystemManager.accessSync(path)

支持最低平台版本号'1040' (minPlatformVersion>='1040')

FileSystemManager.access 的同步版本,判断文件/目录是否存在

参数

属性 类型 是否必填 说明
path string 要判断是否存在的文件/目录路径

错误

错误信息 说明
no such file or directory 文件/目录不存在

FileSystemManager.appendFile(object)

支持最低平台版本号'1040' (minPlatformVersion>='1040')

在文件结尾追加内容

参数

object

属性 类型 是否必填 说明
filePath string 要追加内容的文件路径
data string/ArrayBuffer 要追加的文本或二进制数据
encoding string 指定写入文件的字符编码,当前支持:utf8、binary
success function 接口调用成功的回调函数
fail function 接口调用失败的回调函数
complete function 接口调用结束的回调函数

FileSystemManager.appendFileSync(filePath, data, encoding)

支持最低平台版本号'1040' (minPlatformVersion>='1040')

FileSystemManager.appendFile 的同步版本,在文件结尾追加内容

参数

属性 类型 是否必填 说明
filePath string 要追加内容的文件路径
data string/ArrayBuffer 要追加的文本或二进制数据
encoding string 指定写入文件的字符编码,当前支持:utf8、binary

错误

错误信息 说明
no such file or directory 指定的 filePath 文件不存在, 或是一个目录
permission denied 指定目标文件路径没有写权限

FileSystemManager.copyFile(object)

复制文件异步方式

参数

object

属性 类型 是否必填 说明
srcPath string 源文件路径,只可以是普通文件
destPath string 目标文件路径
success function 接口调用成功的回调函
fail function 接口调用失败的回调函数
complete function 接口调用结束的回调函数

FileSystemManager.copyFileSync(srcPath, destPath)

支持最低平台版本号'1040' (minPlatformVersion>='1040')

FileSystemManager.copyFile 的同步版本,拷贝文件

参数

属性 类型 是否必填 说明
srcPath string 源文件路径,只可以是普通文件
destPath string 目标文件路径

错误

错误信息 说明
no such file or directory 源文件不存在,或目标文件路径的上层目录不存在
permission denied 指定目标文件路径没有写权限

返回值

boolean 值,文件复制成功与否,true 代表成功,false 代表失败

FileSystemManager.getFileInfo(object)

获取本地临时文件或本地用户文件的文件信息

参数

object

属性 类型 是否必填 说明
filePath string 要读取的文件路径
success function 接口调用成功的回调函
fail function 接口调用失败的回调函数
complete function 接口调用结束的回调函数

success(res) 回调函数

res

属性 类型 说明
size number 文件大小,以字节为单位

FileSystemManager.mkdir(object)

创建目录

参数

object

属性 类型 是否必填 说明
dirPath string 创建的目录路径
success function 接口调用成功的回调函
fail function 接口调用失败的回调函数
complete function 接口调用结束的回调函数

FileSystemManager.mkdirSync(dirPath, recursive)

支持最低平台版本号'1040' (minPlatformVersion>='1040')

FileSystemManager.mkdir 的同步版本,创建目录

参数

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

错误

错误信息 说明
no such file or directory 上级目录不存在
permission denied 指定目标文件路径没有写权限
file already exists 有同名文件或目录

FileSystemManager.readFile(object)

读取本地文件内容异步方式

参数

object

属性 类型 是否必填 说明
filePath string 要读取的文件的路径
encoding string 指定读取文件的字符编码,默认为 binary
success function 接口调用成功的回调函
fail function 接口调用失败的回调函数
complete function 接口调用结束的回调函数

success(res) 回调函数

res

属性 类型 说明
data string/ArrayBuffer 读取到的文本或二进制数据

FileSystemManager.readFileSync(filePath, encoding)

支持最低平台版本号'1040' (minPlatformVersion>='1040')

FileSystemManager.readFile 的同步版本,读取文件

参数

属性 类型 是否必填 说明
filePath string 要读取的文件的路径
encoding string 指定读取文件的字符编码,默认为 binary

返回值

属性 类型 说明
data string/ArrayBuffer 读取到的文本或二进制数据

FileSystemManager.rename(object)

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

参数

object

属性 类型 是否必填 说明
oldPath string 源文件路径,可以是普通文件或目录
newPath string 新文件路径
success function 接口调用成功的回调函
fail function 接口调用失败的回调函数
complete function 接口调用结束的回调函数

FileSystemManager.renameSync(oldPath, newPath)

支持最低平台版本号'1040' (minPlatformVersion>='1040')

FileSystemManager.rename 的同步版本,重命名文件

参数

属性 类型 是否必填 说明
oldPath string 源文件路径,可以是普通文件或目录
newPath string 新文件路径

错误

错误信息 说明
no such file or directory 源文件不存在,或目标文件路径的上层目录不存在
permission denied 没有写权限

FileSystemManager.rmdir(object)

删除目录

参数

object

属性 类型 是否必填 说明
dirPath Object 要删除的目录路径
recursive boolean 是否递归删除目录。如果为 true,则删除该目录和该目录下的所有子目录以及文件。
success function 接口调用成功的回调函数
fail function 接口调用失败的回调函数
complete function 接口调用结束的回调函数

FileSystemManager.rmdirSync(dirPath, recursive)

支持最低平台版本号'1040' (minPlatformVersion>='1040')

FileSystemManager.rmdir 的同步版本,移除目录

参数

属性 类型 是否必填 说明
dirPath Object 要删除的目录路径
recursive boolean 是否递归删除目录。如果为 true,则删除该目录和该目录下的所有子目录以及文件。

错误

错误信息 说明
no such file or directory 目录不存在
directory not empty 目录不为空

FileSystemManager.readdir(object)

读取目录内文件列表

参数

object

属性 类型 是否必填 说明
dirPath string 要读取的目录路径
success function 接口调用成功的回调函
fail function 接口调用失败的回调函数
complete function 接口调用结束的回调函数

success(res) 回调函数

res

属性 类型 说明
files Array[string] 指定目录下的文件名数组。

FileSystemManager.readdirSync(dirPath)

支持最低平台版本号'1040' (minPlatformVersion>='1040')

FileSystemManager.readdir 的同步版本,读取目录

参数

属性 类型 是否必填 说明
dirPath string 要读取的目录路径

返回值

属性 类型 说明
files Array 指定目录下的文件名数组

错误

错误信息 说明
no such file or directory 目录不存在
directory not empty 目录不为空

FileSystemManager.unlink(object)

删除文件

参数

object

属性 类型 是否必填 说明
filePath string 要删除的文件路径
success function 接口调用成功的回调函
fail function 接口调用失败的回调函数
complete function 接口调用结束的回调函数

FileSystemManager.unlinkSync(filePath)

支持最低平台版本号'1040' (minPlatformVersion>='1040')

FileSystemManager.unlink 的同步版本,删除文件

参数

属性 类型 是否必填 说明
filePath string 要删除的文件路径

错误

错误信息 说明
no such file or directory 文件不存在

FileSystemManager.unzip(object)

解压文件

参数

object

属性 类型 是否必填 说明
zipFilePath string 源文件路径,只可以是 zip 压缩文件
targetPath string 目标目录路径
success function 接口调用成功的回调函
fail function 接口调用失败的回调函数
complete function 接口调用结束的回调函数

FileSystemManager.writeFile(object)

写文件异步方式

参数

object

属性 类型 是否必填 说明
filePath string 要写入的文件路径
data string/ArrayBuffer 要写入的文本或二进制数据
encoding string 指定写入文件的字符编码 utf8 or binary,默认值为 utf8
append bool 默认为 false,覆盖旧文件
success function 接口调用成功的回调函
fail function 接口调用失败的回调函数,指定目录不存在调用
complete function 接口调用结束的回调函数

FileSystemManager.writeFileSync(filePath, data, encoding, append)

支持最低平台版本号'1040' (minPlatformVersion>='1040')

FileSystemManager.writeFile 的同步版本,写文件

参数

属性 类型 是否必填 说明
filePath string 要写入的文件路径
data string/ArrayBuffer 要写入的文本或二进制数据
encoding string 指定写入文件的字符编码 utf8 or binary,默认值为 utf8
append bool 默认为 false,覆盖旧文件

返回值

boolean 值,代表是否写入文件成功

错误

错误信息 说明
no such file or directory 指定的 filePath 文件不存在, 或是一个目录
permission denied 指定目标文件路径没有写权限

FileSystemManager.saveFile(object)

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

参数

object

属性 类型 是否必填 说明
tempFilePath string 临时存储文件路径
filePath string 要存储的文件路径
success function 接口调用成功的回调函
fail function 接口调用失败的回调函数,指定目录不存在调用
complete function 接口调用结束的回调函数

success(res) 回调函数

res

属性 类型 说明
savedFilePath string 存储后的文件路径

FileSystemManager.saveFileSync(tempFilePath, filePath)

支持最低平台版本号'1040' (minPlatformVersion>='1040')

FileSystemManager.saveFile 的同步版本,保存临时文件到本地。

参数

属性 类型 是否必填 说明
tempFilePath string 临时存储文件路径
filePath string 要存储的文件路径

返回值

属性 类型 说明
savedFilePath string 存储后的文件路径

FileSystemManager.removeSavedFile(object)

删除该 OPPO 小游戏下已保存的本地缓存文件

参数

object

属性 类型 是否必填 说明
filePath string 需要删除的文件路径
success function 接口调用成功的回调函
fail function 接口调用失败的回调函数,指定目录不存在调用
complete function 接口调用结束的回调函数

FileSystemManager.stat(object)

获取文件 Stats 对象

参数

object

属性 类型 是否必填 说明
path string 文件/目录路径
success function 接口调用成功的回调函
fail function 接口调用失败的回调函数
complete function 接口调用结束的回调函数

success(res) 回调函数

res

属性 类型 说明
stat Stats 一个 Stats 对象

FileSystemManager.statSync(path, recursive)

FileSystemManager.stat 的同步版本,获取 stat

参数

属性 类型 是否必填 说明
path string 要删除的目录路径
recursive boolean 是否递归获取目录中所有文件的信息

错误

错误信息 说明
no such file or directory 文件不存在

Stats

属性

mode:文件 mode

size:文件大小

lastAccessedTime:最后一次读取的时间

lastModifiedTime:最后一次修改时间

Stats.isDirectory()

判断当前文件是否一个目录

返回值

boolean 值,表示当前文件是否一个目录

Stats.isFile()

判断当前文件是否一个普通文件

返回值

boolean 值,表示当前文件是否一个普通文件

错误码

名称 说明
ERROR_UNKNOWN -1 未知错误
ERROR_NONE 0 无错误,成功
ERROR_FILE_NO_SUCH_FILE_OR_DIR 1 文件或目录不存在
ERROR_FILE_PERMISSION_DENIED 2 没有权限
ERROR_FILE_ALREADY_EXISTS 3 文件已存在
ERROR_FILE_NOT_A_DIR 4 不是一个目录
ERROR_FILE_NOT_A_FILE 5 不是一个文件
ERROR_FILE_STORAGE_LIMIT 6 存储大小达到上限
ERROR_FILE_DIRECTORY_NOT_EMPTY 7 目录不为空
ERROR_FILE_CREAT_DIR_FAIL 8 目录创建失败

文件 api 通用的 success,fail,complete 的回调参数

  • errCode

错误码, Number 类型

  • errMsg

错误描述, String 类型

© 2020 OPPO. All rights reserved.

results matching ""

    No results matching ""