FileSystemManager
关于文件系统的更多信息,请参考:
access()
access()
是 fileSystemManager 对象的方法,该方法可根据传入的 opts
参数,判断文件/目录是否存在。
fileSystemManager.access(opts)
opts 对象属性说明
属性 | 类型 | 默认值 | 是否必填 | 描述 |
---|---|---|---|---|
opts | Object | 是 | 调用该方法时,要传入的对象参数 | |
opts.path | string | '' |
是 | 文件/目录路径 |
opts.success | function | 否 | 成功回调函数 | |
opts.fail | function | 否 | 失败回调函数 | |
opts.complete | function | 否 | 接口调用完成的回调函数(接口成功、失败都会执行) |
fail
回调函数:
形如 function (res) {...}
,其中:
属性 | 类型 | 描述 |
---|---|---|
res.errMsg | string | 报错信息 |
res.errMsg
的有效值:
值 | 描述 |
---|---|
fail no such file or directory ${path} | 文件/目录不存在 |
complete
回调参数:
当接口执行成功时,其返回值与 success 回调函数的返回值相同;
当接口执行失败时,其返回值与 fail 回调函数的返回值相同。
示例:
const fileSystemManager = swan.getFileSystemManager();
fileSystemManager.access({
path: swan.env.USER_DATA_PATH + '/demo.txt',
success: res => console.log('接口执行成功,demo.txt文件存在'),
fail: res => console.log('接口执行失败', res.errMsg),
complete: res => console.log('接口执行完成')
});
accessSync()
accessSync()
是 fileSystemManager 对象的方法,没有返回值,access()
的同步版本,判断文件/目录是否存在。
fileSystemManager.accessSync(path)
参数值:
参数 | 类型 | 默认值 | 是否必填 | 描述 |
---|---|---|---|---|
path | string | '' |
是 | 文件/目录路径 |
若接口调用失败,会抛出一个标准的 Error 对象,报错信息如下:
message | 描述 |
---|---|
fail no such file or directory ${path} | 文件/目录不存在 |
示例:
const fileSystemManager = swan.getFileSystemManager();
try{
let path=swan.env.USER_DATA_PATH + '/demo.txt';
fileSystemManager.accessSync(path);
// 文件存在
}
catch(e){
// 出错了,文件/目录不存在
console.log(e.message);
}
appendFile()
appendFile()
是 fileSystemManager 对象的方法,该方法可根据传入的 opts
参数,在文件结尾追加内容。
fileSystemManager.appendFile(opts)
opts 对象属性说明
属性 | 类型 | 默认值 | 是否必填 | 描述 |
---|---|---|---|---|
opts | Object | 是 | 调用该方法时,要传入的对象参数 | |
opts.filePath | string | 是 | 文件/目录路径 | |
opts.data | string/ArrayBuffer | 是 | 要追加的文本或二进制内容 | |
opts.encoding | string | utf-8 |
否 | 指定写入文件的字符编码 |
opts.success | function | 否 | 成功回调函数 | |
opts.fail | function | 否 | 失败回调函数 | |
opts.complete | function | 否 | 接口调用完成的回调函数(接口成功、失败都会执行) |
encoding
的有效值:
值 | 描述 |
---|---|
ascii | |
base64 | 对输入的字符串进行 base64 解码再写入 |
hex | 十六进制 |
ucs2/ucs-2/utf16le/utf-16le | 以小端序读取 |
utf-8/utf8 | |
latin1/binary | ISO-8859-1 的别名 |
fail
回调函数:
形如 function (res) {...}
,其中:
属性 | 类型 | 描述 |
---|---|---|
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 | sd 卡没有安装 |
complete
回调参数:
当接口执行成功时,其返回值与 success 回调函数的返回值相同;
当接口执行失败时,其返回值与 fail 回调函数的返回值相同。
示例:
const fileSystemManager = swan.getFileSystemManager();
fileSystemManager.appendFile({
filePath: swan.env.USER_DATA_PATH + '/demo.txt',
data:'hello',
success: res => console.log('接口执行成功,已追加内容'),
fail: res => console.log('接口执行失败', res.errMsg),
complete: res => console.log('接口执行完成')
});
appendFileSync()
appendFileSync()
是 fileSystemManager 对象的方法,没有返回值,appendFile()
的同步版本,在文件末尾追加内容。
fileSystemManager.appendFileSync(path, data, encoding)
参数值:
参数 | 类型 | 默认值 | 是否必填 | 描述 |
---|---|---|---|---|
path | string | 是 | 文件/目录路径 | |
data | string/ArrayBuffer | 是 | 要追加的文本或二进制数据 | |
encoding | string | utf-8 |
指定写入文件的字符编码 |
encoding
的有效值:
值 | 描述 |
---|---|
ascii | |
base64 | 对输入的字符串进行 base64 解码再写入 |
hex | 十六进制 |
ucs2/ucs-2/utf16le/utf-16le | 以小端序读取 |
utf-8/utf8 | |
latin1/binary | ISO-8859-1的别名 |
若接口调用失败,会抛出一个标准的 Error 对象,报错信息如下:
message | 描述 |
---|---|
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 | sd 卡没有安装 |
示例:
const fileSystemManager = swan.getFileSystemManager();
try{
const path=swan.env.USER_DATA_PATH + '/demo.txt';
fileSystemManager.appendFileSync(path,'hello');
// 追加内容成功
}
catch(e){
// 出错了,追加内容失败
console.log(e.message);
}
copyFile()
copyFile()
是 fileSystemManager 对象的方法,用于实现文件拷贝。
fileSystemManager.copyFile(opts)
参数值
参数 | 类型 | 默认值 | 是否必填 | 描述 |
---|---|---|---|---|
opts.srcPath | string |
|
是 | 要被拷贝的源文件名称 |
opts.destPath | string |
|
是 | 拷贝操作的目标文件名 |
opts.success | function | 否 | 成功回调函数 | |
opts.fail | function | 否 | 失败回调函数 | |
opts.complete | function | 否 | 接口调用完成的回调函数(接口成功、失败都会执行) |
fail
回调参数:
参数 | 类型 | 描述 |
---|---|---|
errMsg | string | 报错信息 |
errMsg 的有效值
值 | 描述 |
---|---|
fail no such file or directory | 文件/目录不存在 |
fail permission denied, copyFile ${srcPath} -> ${destPath} | 指定目标文件路径没有写权限 |
srcPath should be string |
参数类型错误,srcPath 参数应该是 string 类型 |
destPath should be string |
参数类型错误,destPath 参数应该是 string 类型 |
complete
回调参数:
当接口执行成功时,其返回值与 success 回调函数的返回值相同;
当接口执行失败时,其返回值与 fail 回调函数的返回值相同。
示例:
const fileSystemManager = swan.getFileSystemManager();
fileSystemManager.copyFile({
srcPath: `${swan.env.USER_DATA_PATH}/demo/demo.txt`,
destPath: `${swan.env.USER_DATA_PATH}/dest/dest.txt`,
success: res => console.log('拷贝成功', res.errMsg),
fail: res => console.log('接口执行失败', res.errMsg),
complete: res => console.log('接口执行完成', res.errMsg)
});
copyFileSync()
copyFileSync()
是 fileSystemManager 对象的方法,用于实现文件拷贝,是 copyFile()
的同步方法。
fileSystemManager.copyFileSync(srcPath, destPath)
参数值
参数 | 类型 | 默认值 | 是否必填 | 描述 |
---|---|---|---|---|
srcPath | string |
|
是 | 要被拷贝的源文件名称 |
destPath | string |
|
是 | 拷贝操作的目标文件名 |
错误信息:
值 | 描述 |
---|---|
fail no such file or directory | 文件/目录不存在 |
fail permission denied, copyFileSync ${srcPath} -> ${destPath} | 指定目标文件路径没有写权限 |
The argument must be string | 参数类型错误,srcPath 参数应该是 string 类型 |
The argument must be string | 参数类型错误,destPath 参数应该是 string 类型 |
示例:
const fileSystemManager = swan.getFileSystemManager();
try {
fileSystemManager.copyFileSync(
`${swan.env.USER_DATA_PATH}/demo/demo.txt`,
`${swan.env.USER_DATA_PATH}/dest/dest.txt`
);
}
catch (e) {
// 出错信息
console.log(e.message);
}
getFileInfo()
getFileInfo()
是 fileSystemManager 对象的方法,该方法可获取到本地临时文件的相关信息, 并在 success 回调函数中返回该文件大小。
fileSystemManager.getFileInfo(opts)
参数值:
属性 | 类型 | 默认值 | 是否必填 | 描述 |
---|---|---|---|---|
opts | Object | 是 | 调用该方法时,要传入的对象参数 | |
opts.filePath | string | 是 | 读取的文件路径 | |
opts.success | function | 否 | 成功回调函数 | |
opts.fail | function | 否 | 失败回调函数 | |
opts.complete | function | 否 | 接口调用完成的回调函数(接口成功、失败都会执行) |
success
回调函数:
形如 function (res) {...}
,其中:
属性 | 类型 | 描述 |
---|---|---|
res.size | number | 文件大小,以字节为单位 |
fail
回调函数:
形如 function (res) {...}
,其中:
属性 | 类型 | 描述 |
---|---|---|
res.errMsg | string | fail file not exist 指定的路径下该文件不存在 |
示例:
const fileSystemManager = swan.getFileSystemManager();
fileSystemManager.getFileInfo({
filePath: swan.env.USER_DATA_PATH + '/demo.txt',
success: res => console.log('接口执行成功,返回文件的size值', res.size),
fail: res => console.log('接口执行失败', res.errMsg),
complete: res => console.log('接口执行完成', res);
});
readdir()
readdir()
是 fileSystemManager 对象的方法,该方法可读取本地指定路径下的目录内文件列表。
fileSystemManager.readdir(opts)
参数值:
属性 | 类型 | 默认值 | 是否必填 | 描述 |
---|---|---|---|---|
opts | Object | 是 | 调用该方法时,要传入的对象参数 | |
opts.dirPath | string | 是 | 想要读取的文件所在的目录 | |
opts.success | function | 否 | 成功回调函数 | |
opts.fail | function | 否 | 失败回调函数 | |
opts.complete | function | 否 | 接口调用完成的回调函数(接口成功、失败都会执行) |
success
回调函数:
形如 function (res) {...}
,其中:
属性 | 类型 | 描述 |
---|---|---|
res.files | Array | 指定目录下的文件列表 |
files[].fileItem | string | 文件列表里的文件项,存储着路径 |
fail
回调函数:
形如 function (res) {...}
,其中:
属性 | 类型 | 描述 |
---|---|---|
res.errMsg | string | fail no such file or directory ${dirPath} 指定的路径下目录不存在,或读取的是文件非目录 |
fail permission denied, open ${dirPath} 指定目录不在本地路径下,没有读权限 |
示例:
const fileSystemManager = swan.getFileSystemManager();
fileSystemManager.readdir({
dirPath: swan.env.USER_DATA_PATH + '/demo',
success: res => console.log('接口执行成功,返回文件的files数组', res.files),
fail: res => console.log('接口执行失败', res.errMsg),
complete: res => console.log('接口执行完成', res);
});
readdirSync()
readdirSync()
是 fileSystemManager.readdir() 的同步方法,该方法可读取本地指定路径下的目录。
fileSystemManager.readdirSync(dirPath)
参数值:
属性 | 类型 | 默认值 | 是否必填 | 描述 |
---|---|---|---|---|
dirPath | string | 是 | 想要读取的文件所在的目录 |
返回值:
若接口调用成功,则返回
属性 | 类型 | 描述 |
---|---|---|
files | Array | 指定目录下的文件列表 |
files.fileItem | string | 文件列表里的文件项,存储着路径 |
若接口调用失败,则返回
属性 | 类型 | 描述 |
---|---|---|
err | Object | 错误对象 |
err.message | string | 错误信息,可能是:'fail no such file or directory ${dirPath}' 表示指定的路径下目录不存在,或读取的是文件非目录;或 'fail permission denied, open ${dirPath}' 表示指定目录不在本地路径下,没有读权限 |
示例:
const fileSystemManager = swan.getFileSystemManager();
try {
let files = fileSystemManager.readdirSync(swan.env.USER_DATA_PATH + '/demo');
console.log('该路径对应文件的files数组', files);
} catch(e) {
console.log('接口执行失败', e.message);
}
readFile()
fileSystemManager.readFile()
用于读文件,是 readFileSync()
的异步版本。
fileSystemManager.readFile(opts)
opts
对象属性说明
属性 | 类型 | 默认值 | 是否必填 | 描述 |
---|---|---|---|---|
opts.filePath | string | 是 | 文件/目录路径。“本地用户文件”须以 swan.env.USER_DATA_PATH 开头(如 'swan.env.USER_DATA_PATH' + '/demo.txt' ),否则认为是“代码包文件”(如 'asset/bg.jpg' )。不支持相对路径。 |
|
opts.encoding | string | 否 | 指定读文件的字符编码。如果不指定,则读出 ArrayBuffer ;如果指定,则读出 string 。 |
|
opts.success | function | 否 | 成功回调函数。 | |
opts.fail | function | 否 | 失败回调函数。 | |
opts.complete | function | 否 | 接口调用完成的回调函数(接口成功、失败都会执行)。 |
opts.encoding
的有效值:
值 | 描述 |
---|---|
ascii | |
base64 | 对读取的内容进行 base64 编码并返回。 |
hex | 十六进制 |
ucs2/ucs-2/utf16le/utf-16le | 以小端序读取。 |
utf-8/utf8 | |
latin1/binary | ISO-8859-1 的别名。 |
success
回调函数:
形如 function (res) {...}
,其中:
属性 | 类型 | 描述 |
---|---|---|
res.data | string/ArrayBuffer | 如果传入了 opts.encoding 则返回 string ,否则返回 arrayBuffer (文件二进制内容) |
fail
回调函数:
形如 function (res) {...}
,其中:
属性 | 类型 | 描述 |
---|---|---|
res.errMsg | string | 报错信息 |
res.errMsg
的有效值:
值 | 描述 |
---|---|
fail no such file or directory ${filePath} | 文件/目录不存在。 |
fail permission denied, open ${dirPath} | 指定的 filePath 路径没有写权限。 |
complete
回调函数 function (res) {...}
:
当接口执行成功时,其 res
与 success
的 res
相同;
当接口执行失败时,其 res
与 fail
的 res
相同。
示例:
const fs = swan.getFileSystemManager();
fs.readFile({
filePath: `${swan.env.USER_DATA_PATH}/demo.txt`,
success: res => {
// res.data 中为 arrayBuffer,文件内容。
},
fail: res => {
// res.errMsg 为失败信息。
},
complete: () => {
// 接口执行完成
}
});
fs.readFile({
filePath: `${swan.env.USER_DATA_PATH}/demo.txt`,
encoding: 'utf8',
success: res => {
// res.data 中为 string,文件内容。
},
fail: res => {
// res.errMsg 为失败信息。
},
complete: () => {
// 接口执行完成
}
});
readFileSync()
fileSystemManager.readFileSync()
用于读文件,是 readFile()
的同步版本。
let data = fileSystemManager.readFileSync(filePath, encoding)
参数值:
参数 | 类型 | 默认值 | 是否必填 | 描述 |
---|---|---|---|---|
filePath | string | 是 | 文件/目录路径。“本地用户文件”须以 swan.env.USER_DATA_PATH 开头(如 'swan.env.USER_DATA_PATH' + '/demo.txt' ),否则认为是“代码包文件”(如 'asset/bg.jpg' )。不支持相对路径。 |
|
encoding | string | 否 | 指定写入文件的字符编码。如果不指定,则读出 ArrayBuffer ;如果指定,则读出 string 。 |
encoding
的有效值:
值 | 描述 |
---|---|
ascii | |
base64 | 对读取的内容进行 base64 编码并返回。 |
hex | 十六进制。 |
ucs2/ucs-2/utf16le/utf-16le | 以小端序读取 |
utf-8/utf8 | |
latin1/binary | ISO-8859-1 的别名 |
返回值:
类型 | 描述 |
---|---|
string/ArrayBuffer | 如果 encoding 被指定,则返回 string ;如果 encoding 没有被指定,则返回 ArrayBuffer 。 |
若接口调用失败,会抛出一个标准的 Error 对象,报错信息如下:
error.message | 描述 |
---|---|
fail no such file or directory, open ${filePath} | 指定的 filePath 文件不存在 |
fail permission denied, open ${dirPath} | 指定的 filePath 路径没有写权限 |
示例:
const fs = swan.getFileSystemManager();
try {
let data = fs.readFileSync(
`${swan.env.USER_DATA_PATH}/demo.txt`,
'utf8'
);
// 读用户文件成功,文件内容为 hello
}
catch (err) {
// 出错了,读文件失败
console.log(err.message);
}
try {
let data = fs.writeFileSync(
`${swan.env.USER_DATA_PATH}/demo.bin`
);
// 读用户文件成功,文件内容为 arrayBuffer 指定的内容。
}
catch (err) {
// 出错了,读文件失败
console.log(err.message);
}
try {
let gameJSON = fs.writeFileSync(
`game.json`,
'utf8'
);
// 读代码文件成功。
}
catch (err) {
// 出错了,读文件失败
console.log(err.message);
}
rename()
rename()
是 fileSystemManager 对象的方法,该方法可根据传入的 opts
参数,重命名文件,可以把文件从 oldPath
移动到 newPath
。
fileSystemManager.rename(opts)
opts 对象属性说明
属性 | 类型 | 默认值 | 是否必填 | 描述 |
---|---|---|---|---|
opts | Object | 是 | 调用该方法时,要传入的对象参数 | |
opts.oldPath | string | '' |
是 | 源文件/源目录路径 |
opts.newPath | string | '' |
是 | 目标路径 |
opts.success | function | 否 | 成功回调函数 | |
opts.fail | function | 否 | 失败回调函数 | |
opts.complete | function | 否 | 接口调用完成的回调函数(接口成功、失败都会执行) |
fail
回调函数:
形如 function (res) {...}
,其中:
属性 | 类型 | 描述 |
---|---|---|
res.errMsg | string | 报错信息 |
res.errMsg
的有效值:
值 | 描述 |
---|---|
fail permission denied, rename ${oldPath} -> ${newPath} | 指定源文件或目标文件没有写权限 |
fail no such file or directory, rename ${oldPath} -> ${newPath} | 源文件不存在,或目标文件路径的上层目录不存在 |
complete
回调参数:
当接口执行成功时,其返回值与 success 回调函数的返回值相同;
当接口执行失败时,其返回值与 fail 回调函数的返回值相同。
示例:
const fileSystemManager = swan.getFileSystemManager();
fileSystemManager.rename({
oldPath:'./a.txt',
newPath:'./b',
success: res => console.log('接口执行成功,a.txt转移到了b目录下'),
fail: res => console.log('接口执行失败', res.errMsg),
complete: res => console.log('接口执行完成');
});
renameSync()
renameSync()
是 fileSystemManager 对象的方法,没有返回值,rename()
的同步版本,重命名文件,可以把文件从 oldPath
移动到 newPath
。
fileSystemManager.renameSync(oldPath, newPath)
参数值
参数 | 类型 | 默认值 | 是否必填 | 描述 |
---|---|---|---|---|
oldPath | string | 是 | 源文件/源目录路径 | |
newPath | string | 是 | 目标路径 |
若接口调用失败,会抛出一个标准的 Error 对象,报错信息如下:
message | 描述 |
---|---|
fail permission denied, rename ${oldPath} -> ${newPath} | 指定源文件或目标文件没有写权限 |
fail no such file or directory, rename ${oldPath} -> ${newPath} | 源文件不存在,或目标文件路径的上层目录不存在 |
示例:
const fileSystemManager = swan.getFileSystemManager();
try{
fileSystemManager.renameSync('./a.txt','./b');
// 重命名成功
}
catch(e){
// 出错了,重命名失败
console.log(e.message);
}
saveFile()
saveFile()
是 fileSystemManager 对象的方法,该方法可根据传入的 opts
参数,保存临时文件到本地。此接口会移动临时文件,因此调用成功后, tempFilePath
将不可用。
fileSystemManager.saveFile(opts)
opts 对象属性说明
属性 | 类型 | 默认值 | 是否必填 | 描述 |
---|---|---|---|---|
opts | Object | 是 | 调用该方法时,要传入的对象参数 | |
opts.tempFilePath | string | 是 | 临时存储文件路径 | |
opts.filePath | string | 否 | 要存储的文件路径 | |
opts.success | function | 否 | 成功回调函数 | |
opts.fail | function | 否 | 失败回调函数 | |
opts.complete | function | 否 | 接口调用完成的回调函数(接口成功、失败都会执行) |
success
回调函数:
形如 function (res) {...}
,其中:
属性 | 类型 | 描述 |
---|---|---|
res.savedFilePath | string | 存储后的文件路径 |
fail
回调函数:
形如 function (res) {...}
,其中:
属性 | 类型 | 描述 |
---|---|---|
res.errMsg | string | 报错信息 |
res.errMsg
的有效值:
值 | 描述 |
---|---|
fail no such file or directory ${dirPath} | 上级目录不存在 |
fail tempFilePath file not exist | 指定的 tempFilePath 找不到文件 |
fail permission denied, open ${filePath} | 指定的 filePath 路径没有写权限 |
complete
回调参数:
当接口执行成功时,其返回值与 success 回调函数的返回值相同;
当接口执行失败时,其返回值与 fail 回调函数的返回值相同。
示例:
const fileSystemManager = swan.getFileSystemManager();
fileSystemManager.saveFile({
tempFilePath:'/usr/temp.txt',
filePath:'/usr/demo.txt',
success: res => {
// 存储后的文件路径
console.log(res.savedFilePath);
},
fail: res => console.log('接口执行失败', res.errMsg),
complete: res => console.log('接口执行完成')
});
saveFileSync()
saveFileSync()
是 fileSystemManager 对象的方法,saveFile()
的同步版本,保存临时文件到本地。此接口会移动临时文件,因此调用成功后, tempFilePath
将不可用。
fileSystemManager.saveFileSync(tempFilePath, filePath)
参数值:
参数 | 类型 | 默认值 | 是否必填 | 描述 |
---|---|---|---|---|
tempFilePath | string | 是 | 临时存储文件路径 | |
filePath | string | 否 | 要存储的文件路径 |
返回值:
string
类型的值:
值 | 类型 | 描述 |
---|---|---|
savedFilePath | string | 存储后的文件路径径 |
若接口调用失败,会抛出一个标准的 Error 对象,报错信息如下:
message | 描述 |
---|---|
fail no such file or directory ${path} | 文件/目录不存在 |
fail tempFilePath file not exist | 指定的 tempFilePath 找不到文件 |
fail permission denied, open ${filePath} | 指定的 filePath 路径没有写权限 |
示例:
const fileSystemManager = swan.getFileSystemManager();
try{
let savedFilePath=fileSystemManager.saveFileSync(tempFilePath,filePath);
// 临时文件已经被保存,存储后的文件路径为saveFilePath
}
catch(e){
// 出错了
console.log(e.message);
}
stat()
stat()
是 fileSystemManager 对象的方法,该方法可根据传入的 path 参数,生成文件的 Stats
对象,并在 success 回调函数中将该 Stats
对象返回。只能获取本地文件和本地临时文件。
fileSystemManager.stat(opts)
参数值:
属性 | 类型 | 默认值 | 是否必填 | 描述 |
---|---|---|---|---|
opts | Object | 是 | 调用该方法时,要传入的对象参数 | |
opts.path | string | '' |
是 | 文件/目录路径 |
opts.success | function | 否 | 成功回调函数 | |
opts.fail | function | 否 | 失败回调函数 | |
opts.complete | function | 否 | 接口调用完成的回调函数(接口成功、失败都会执行) |
success
回调函数:
形如 function (res) {...}
,其中:
属性 | 类型 | 描述 |
---|---|---|
res.stats | Stats | 一个 Stats 类型的对象 |
fail
回调函数:
形如 function (res) {...}
,其中:
属性 | 类型 | 描述 |
---|---|---|
res.errMsg | string | fail no such file or directory ${path} 指定的路径下文件不存在 |
fail permission denied, open ${path} 指定目录不在本地路径下,没有读权限 |
complete
回调参数:
当接口执行成功时,其返回值与 success 回调函数的返回值相同;
当接口执行失败时,其返回值与 fail 回调函数的返回值相同。
其他方法:
stats.isDirectory()
判断当前文件是否一个目录,返回 boolean 值
stats.isFile()
判断当前文件是否一个普通文件,返回 boolean 值
示例:
const fileSystemManager = swan.getFileSystemManager();
fileSystemManager.stat({
path: swan.env.USER_DATA_PATH + '/demo.txt',
success: res => {
let stats = res.stats;
console.log('接口执行成功,返回文件的Stats对象', res.stats);
console.log('当前文件是否目录', stats.isDirectory());
console.log('当前文件是否普通文件', stats.isFile());
},
fail: res => console.log('接口执行失败', res.errMsg),
complete: res => console.log('接口执行完成', res);
});
statSync()
statSync()
是 fileSystemManager.stat() 的同步方法,该方法可根据传入的 path
参数,生成文件的 Stats
对象,并将该对象返回。只能获取本地文件和本地临时文件。
fileSystemManager.statSync(path)
参数值:
属性 | 类型 | 是否必填 | 描述 |
---|---|---|---|
path | string | 是 | 目录/文件路径 |
返回值:
若接口调用成功,则返回该路径对应的文件的 Stats 对象。
Stats stats
若接口调用失败,则返回:
属性 | 类型 | 描述 |
---|---|---|
err | Object | 错误对象 |
err.message | string | 错误信息,可能是:'fail no such file or directory ${path}' 表示指定的路径下文件不存在,或读取的是文件非目录;或 'fail permission denied, open ${path}' 表示指定目录不在本地路径下,没有读权限 |
其他方法:
stats.isDirectory()
判断当前文件是否一个目录,返回 boolean 值
stats.isFile()
判断当前文件是否一个普通文件,返回 boolean 值
示例:
const fileSystemManager = swan.getFileSystemManager();
try {
let stats = fileSystemManager.statSync(swan.env.USER_DATA_PATH + '/demo.txt');
console.log('该路径对应的文件的Stats对象', stats);
console.log('当前文件是否目录', stats.isDirectory());
console.log('当前文件是否普通文件', stats.isFile());
} catch(e) {
console.log('接口执行失败', e.message);
}
mkdir()
mkdir()
是 fileSystemManager 对象的方法,该方法可根据传入的 dirPath
参数,在对应目录下创建目录。
fileSystemManager.mkdir(opts)
参数值:
属性 | 类型 | 默认值 | 是否必填 | 描述 |
---|---|---|---|---|
opts | Object | {} | 是 | 调用该方法时,要传入的对象参数 |
opts.dirPath | string | 是 | 创建的目录路径 | |
opts.success | function | 否 | 成功回调函数 | |
opts.fail | function | 否 | 失败回调函数 | |
opts.complete | function | 否 | 接口调用完成的回调函数(接口成功、失败都会执行) |
success
回调函数:
形如 function (res) {...}
,其中:
属性 | 类型 | 描述 |
---|---|---|
res.errMsg | string | 创建成功信息 |
fail
回调函数:
形如 function (res) {...}
,其中:
属性 | 类型 | 描述 |
---|---|---|
res.errMsg | string | 报错信息 |
res.errMsg
的有效值:
值 | 描述 |
---|---|
fail no such file or directory ${dirPath} | 上级目录不存在 |
fail permission denied, open ${dirPath} | 指定的 filePath 路径没有写权限 |
fail dirPath already exists ${dirPath} | 有同名文件或目录 |
complete
回调参数:
当接口执行成功时,其返回值与 success 回调函数的返回值相同;
当接口执行失败时,其返回值与 fail 回调函数的返回值相同。
示例:
const fileSystemManager = swan.getFileSystemManager();
fileSystemManager.mkdir({
dirPath: `${swan.env.USER_DATA_PATH}/demo`,
success: res => console.log('接口执行成功', res),
fail: res => console.log('接口执行失败', res),
complete: res => console.log('接口执行完成', res)
});
mkdirSync()
fileSystemManager.mkdir()
对应的同步版本。
fileSystemManager.mkdirSync(dirPath)
参数值:
属性 | 类型 | 默认值 | 是否必填 | 描述 |
---|---|---|---|---|
dirPath | string | 是 | 创建的目录路径 |
errMsg
的有效值:
值 | 描述 |
---|---|
fail no such file or directory ${dirPath} | 上级目录不存在 |
fail permission denied, open ${dirPath} | 指定的 filePath 路径没有写权限 |
fail file already exists ${dirPath} | 有同名文件或目录 |
示例:
const fileSystemManager = swan.getFileSystemManager();
fileSystemManager.mkdirSync(`${swan.env.USER_DATA_PATH}/demo`);
rmdir()
rmdir()
是 fileSystemManager 对象的方法,该方法可根据传入的 dirPath
参数,删除对应目录。
fileSystemManager.rmdir(opts)
参数值:
属性 | 类型 | 默认值 | 是否必填 | 描述 |
---|---|---|---|---|
opts | Object | {} | 是 | 调用该方法时,要传入的对象参数 |
opts.dirPath | string | 是 | 要删除的目录路径 | |
opts.recursive | Boolean | false | 否 | 是否递归删除目录。如果为 true,则删除该目录和该目录下的所有子目录以及文件 |
opts.success | function | 否 | 成功回调函数 | |
opts.fail | function | 否 | 失败回调函数 | |
opts.complete | function | 否 | 接口调用完成的回调函数(接口成功、失败都会执行) |
success
回调函数:
形如 function (res) {...}
,其中:
属性 | 类型 | 描述 |
---|---|---|
res.errMsg | string | 创建成功信息 |
fail
回调函数:
形如 function (res) {...}
,其中:
属性 | 类型 | 描述 |
---|---|---|
res.errMsg | string | 报错信息 |
res.errMsg
的有效值:
值 | 描述 |
---|---|
fail no such file or directory ${dirPath} | 目录不存在 |
fail directory not empty | 目录不为空 |
fail permission denied, open ${dirPath} | 指定的 dirPath 路径没有写权限 |
complete
回调参数:
当接口执行成功时,其返回值与 success 回调函数的返回值相同;
当接口执行失败时,其返回值与 fail 回调函数的返回值相同。
示例:
const fileSystemManager = swan.getFileSystemManager();
fileSystemManager.rmdir({
dirPath: `${swan.env.USER_DATA_PATH}/demo`,
recursive: true,
success: res => console.log('接口执行成功', res),
fail: res => console.log('接口执行失败', res),
complete: res => console.log('接口执行完成', res)
});
rmdirSync()
fileSystemManager.rmdir()
对应的同步版本。
fileSystemManager.rmdirSync(dirPath[, recursive])
参数值:
属性 | 类型 | 默认值 | 是否必填 | 描述 |
---|---|---|---|---|
dirPath | string | 是 | 要删除的目录路径 | |
recursive | Boolean | false | 否 | 是否递归删除目录。如果为 true,则删除该目录和该目录下的所有子目录以及文件 |
errMsg
的有效值:
值 | 描述 |
---|---|
fail no such file or directory ${dirPath} | 目录不存在 |
fail directory not empty | 目录不为空 |
fail permission denied, open ${dirPath} | 指定的 dirPath 路径没有写权限 |
示例:
const fileSystemManager = swan.getFileSystemManager();
fileSystemManager.rmdirSync(`${swan.env.USER_DATA_PATH}/demo`, true);
unlink()
unlink()
是 fileSystemManager 对象的方法,该方法可根据传入的 filePath
参数,删除对应目录下的文件。
fileSystemManager.unlink(opts)
参数值:
属性 | 类型 | 默认值 | 是否必填 | 描述 |
---|---|---|---|---|
opts | Object | {} | 是 | 调用该方法时,要传入的对象参数 |
opts.filePath | string | 是 | 要删除的文件路径 | |
opts.success | function | 否 | 成功回调函数 | |
opts.fail | function | 否 | 失败回调函数 | |
opts.complete | function | 否 | 接口调用完成的回调函数(接口成功、失败都会执行) |
success
回调函数:
形如 function (res) {...}
,其中:
属性 | 类型 | 描述 |
---|---|---|
res.errMsg | string | 创建成功信息 |
fail
回调函数:
形如 function (res) {...}
,其中:
属性 | 类型 | 描述 |
---|---|---|
res.errMsg | string | 报错信息 |
res.errMsg
的有效值:
值 | 说明 |
---|---|
fail permission denied, open ${filePath} | 指定的 filePath 路径没有读权限 |
fail no such file or directory ${filePath} | 文件不存在 |
fail operation not permitted, unlink ${filePath} | 传入的 filePath 是一个目录 |
complete
回调参数:
当接口执行成功时,其返回值与 success 回调函数的返回值相同;
当接口执行失败时,其返回值与 fail 回调函数的返回值相同。
示例:
const fileSystemManager = swan.getFileSystemManager();
fileSystemManager.unlink({
filePath: `${swan.env.USER_DATA_PATH}/demo.txt`,
success: res => console.log('接口执行成功', res),
fail: res => console.log('接口执行失败', res),
complete: res => console.log('接口执行完成', res)
});
unlinkSync()
fileSystemManager.unlink()
对应的同步版本。
fileSystemManager.unlinkSync(filePath)
参数值:
属性 | 类型 | 默认值 | 是否必填 | 描述 |
---|---|---|---|---|
filePath | string | 是 | 要删除的文件路径 |
errMsg
的有效值:
值 | 描述 |
---|---|
fail permission denied, open ${filePath} | 指定的 filePath 路径没有读权限 |
fail no such file or directory ${filePath} | 文件不存在 |
fail operation not permitted, unlink ${filePath} | 传入的 filePath 是一个目录 |
示例:
const fileSystemManager = swan.getFileSystemManager();
fileSystemManager.unlinkSync(`${swan.env.USER_DATA_PATH}/demo.txt`);
unzip()
unzip()
是 fileSystemManager 对象的方法,该方法可根据传入的 zipFilePath
参数,解压对应目录下的文件。
fileSystemManager.unzip(opts)
参数值:
属性 | 类型 | 默认值 | 是否必填 | 描述 |
---|---|---|---|---|
opts | Object | {} | 是 | 调用该方法时,要传入的对象参数 |
opts.zipFilePath | string | 是 | 源文件路径,必须是 zip 压缩文件。文件必须位于用户目录下,参考示例 | |
opts.targetPath | string | 是 | 解压目标路径,目标目录必须位于用户目录下,参考示例 | |
opts.success | function | 否 | 成功回调函数 | |
opts.fail | function | 否 | 失败回调函数 | |
opts.complete | function | 否 | 接口调用完成的回调函数(接口成功、失败都会执行) |
success
回调函数:
形如 function (res) {...}
,其中:
属性 | 类型 | 描述 |
---|---|---|
res.errMsg | string | 创建成功信息 |
fail
回调函数:
形如 function (res) {...}
,其中:
属性 | 类型 | 描述 |
---|---|---|
res.errMsg | string | 报错信息 |
res.errMsg
的有效值:
值 | 描述 |
---|---|
fail permission denied, unzip ${zipFilePath} -> ${targetPath} | 指定目标文件路径没有写权限 |
fail no such file or directory, unzip ${zipFilePath} -> "${targetPath} | 源文件不存在,或目标文件路径的上层目录不存在 |
complete
回调参数:
当接口执行成功时,其返回值与 success 回调函数的返回值相同;
当接口执行失败时,其返回值与 fail 回调函数的返回值相同。
示例:
const fileSystemManager = swan.getFileSystemManager();
fileSystemManager.unzip({
zipFilePath: `${swan.env.USER_DATA_PATH}/demo/a.zip`,
targetPath: `${swan.env.USER_DATA_PATH}/demo/b`,
success: res => console.log('接口执行成功', res),
fail: res => console.log('接口执行失败', res),
complete: res => console.log('接口执行完成', res)
});
writeFile()
fileSystemManager.writeFile()
用于写文件,是 writeFileSync()
的异步版本。
fileSystemManager.writeFile(opts)
opts
对象属性说明
属性 | 类型 | 默认值 | 是否必填 | 描述 |
---|---|---|---|---|
opts.filePath | string | 是 | 文件/目录路径。须以 swan.env.USER_DATA_PATH 开头(如 'swan.env.USER_DATA_PATH' + '/demo.txt' ) |
|
opts.data | string/ArrayBuffer | 是 | 要写入的文本或二进制数据 | |
opts.encoding | string | 否 | 指定写入文件的字符编码。如果 data 为 ArrayBuffer ,就不要传入 encoding 。如果 data 为 string ,encoding 默认为 utf8 |
|
opts.success | function | 否 | 成功回调函数 | |
opts.fail | function | 否 | 失败回调函数 | |
opts.complete | function | 否 | 接口调用完成的回调函数(接口成功、失败都会执行) |
opts.encoding
的有效值:
值 | 描述 |
---|---|
ascii | |
base64 | 对输入的字符串进行 base64 解码再写入 |
hex | 十六进制 |
ucs2/ucs-2/utf16le/utf-16le | 以小端序读取 |
utf-8/utf8 | |
latin1/binary | ISO-8859-1 的别名 |
fail
回调函数:
形如 function (res) {...}
,其中:
属性 | 类型 | 描述 |
---|---|---|
res.errMsg | string | 报错信息 |
res.errMsg
的有效值:
值 | 描述 |
---|---|
fail no such file or directory ${filePath} | 文件/目录不存在 |
fail permission denied, open ${dirPath} | 指定的 filePath 路径没有写权限 |
complete
回调函数 function (res) {...}
:
当接口执行成功时,其 res
与 success
的 res
相同;
当接口执行失败时,其 res
与 fail
的 res
相同。
示例:
const fs = swan.getFileSystemManager();
fs.writeFile({
filePath: `${swan.env.USER_DATA_PATH}/demo.txt`,
data: 'hello',
success: res => {
// 写入成功。
},
fail: res => {
// res.errMsg 为失败信息。
},
complete: () => {
// 接口执行完成
}
});
let bin = new Uint8Array(100);
for (let i = 0; i < bin.length; i++) {
bin[i] = Math.round(Math.random() * 50);
}
fs.writeFile({
filePath: `${swan.env.USER_DATA_PATH}/demo.bin`,
data: bin.buffer,
success: res => {
// 写入成功。
},
fail: res => {
// res.errMsg 为失败信息。
},
complete: () => {
// 接口执行完成。
}
});
writeFileSync()
fileSystemManager.writeFileSync()
用于写文件,是 writeFile()
的同步版本。
fileSystemManager.writeFileSync(filePath, data, encoding)
参数值:
参数 | 类型 | 默认值 | 是否必填 | 描述 |
---|---|---|---|---|
filePath | string | 是 | 文件/目录路径。须以 swan.env.USER_DATA_PATH 开头(如 'swan.env.USER_DATA_PATH' + '/demo.txt' ) |
|
data | string/ArrayBuffer | 是 | 要写入的文本或二进制数据 | |
encoding | string | 否 | 指定写入文件的字符编码。如果 data 为 ArrayBuffer ,就不要传入 encoding 。如果 data 为 string ,encoding 默认为 utf8 |
encoding
的有效值:
值 | 描述 |
---|---|
ascii | |
base64 | 对输入的字符串进行 base64 解码再写入 |
hex | 十六进制 |
ucs2/ucs-2/utf16le/utf-16le | 以小端序读取 |
utf-8/utf8 | |
latin1/binary | ISO-8859-1 的别名 |
若接口调用失败,会抛出一个标准的 Error 对象,报错信息如下:
error.message | 描述 |
---|---|
fail no such file or directory, open ${filePath} | 指定的 filePath 文件不存在 |
fail permission denied, open ${dirPath} | 指定的 filePath 路径没有写权限 |
示例:
const fs = swan.getFileSystemManager();
try {
fs.writeFileSync(
`${swan.env.USER_DATA_PATH}/demo.txt`,
'hello',
'utf8'
);
// 写文件成功,文件内容为 hello
}
catch (err) {
// 出错了,写文件失败
console.log(err.message);
}
try {
let bin = new Uint8Array(100);
for (let i = 0; i < bin.length; i++) {
bin[i] = Math.round(Math.random() * 50);
}
fs.writeFileSync(
`${swan.env.USER_DATA_PATH}/demo.bin`,
bin.buffer
);
// 写文件成功,文件内容为 arrayBuffer 指定的内容。
}
catch (err) {
// 出错了,写文件失败
console.log(err.message);
}