URI 用于标识应用资源和文件,通过 URI 来访问应用资源和文件。
资源类型 | URI | 只读 | 示例 | 说明 |
---|---|---|---|---|
应用资源 | /path | 是 | /image/logo.png | 注意大小写 |
Cache | internal://cache/path | 否 | internal://cache/fetch-123456.png | |
Files | internal://files/path | 否 | internal://files/image/demo.png | |
Temp | internal://tmp/path | 是 | internal://tmp/xxxxx | 由系统动态生成 |
internal URI 表示的是应用私有文件,即在指定 internal URI 时,无需指定应用标识,同一个 internal URI 对于不同的应用会指向不同的文件。
获取全局唯一的文件管理器
文件管理器
保存临时文件到本地。此接口会移动临时文件,因此调用成功后,tempFilePath 将不可用。
Object object
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
tempFilePath | String | 是 | 临时存储文件路径 (本地路径) | |
filePath | String | 否 | 要存储的文件路径 (本地路径) | |
success | Function | 否 | 接口调用成功的回调函数 | |
fail | Function | 否 | 接口调用失败的回调函数 | |
complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
属性 | 类型 | 说明 |
---|---|---|
savedFilePath | String | 存储后的文件路径 (本地路径) |
FileSystemManager.saveFile 的同步版本
临时存储文件路径 (本地路径)
要存储的文件路径 (本地路径)
存储后的文件路径 (本地路径)
创建目录
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 | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
FileSystemManager.mkdir 的同步版本
创建的目录路径 (本地路径)
是否在递归创建该目录的上级目录后再创建该目录。如果对应的上级目录已经存在,则不创建该上级目录。如 dirPath 为 a/b/c/d 且 recursive 为 true,将创建 a 目录,再在 a 目录下创建 b 目录,以此类推直至创建 a/b/c 目录下的 d 目录。
const fs = qg.getFileSystemManager();
fs.mkdir({
dirPath: `${qg.env.USER_DATA_PATH}/example`,
recursive: false,
success(res) {
console.log(res);
},
fail(res) {
console.error(res);
}
});
// 同步接口
try {
fs.mkdirSync(`${qg.env.USER_DATA_PATH}/example`, false);
} catch (e) {
console.error(e);
}
读取本地文件内容。单个文件大小上限为 100M。
Object object
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
filePath | String | 是 | 要读取的文件的路径 (本地路径) | |
encoding | String | 否 | 指定读取文件的字符编码,如果不传 encoding,则以 ArrayBuffer 格式读取文件的二进制内容 | |
position | Number | 否 | 从文件指定位置开始读,如果不指定,则从文件头开始读。读取的范围应该是左闭右开区间 [position, position+length)。有效范围:[0, fileLength - 1]。单位:byte | |
length | Number | 否 | 指定文件的长度,如果不指定,则读到文件末尾。有效范围:[1, fileLength]。单位:byte | |
success | Function | 否 | 接口调用成功的回调函数 | |
fail | Function | 否 | 接口调用失败的回调函数 | |
complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
属性 | 类型 |
---|---|
ascii | |
base64 | |
binary | |
hex | |
utf-8 | |
utf8 | |
latin1 | |
ucs2 | 以小端序读取 |
ucs-2 | 以小端序读取 |
utf16le | 以小端序读取 |
utf-16le | 以小端序读取 |
属性 | 类型 | 说明 |
---|---|---|
data | String/ArrayBuffer | 文件内容 |
FileSystemManager.readFile 的同步版本
要读取的文件的路径 (本地路径)
指定读取文件的字符编码,如果不传 encoding,则以 ArrayBuffer 格式读取文件的二进制内容
属性 | 类型 |
---|---|
ascii | |
base64 | |
binary | |
hex | |
utf-8 | |
utf8 | |
latin1 | |
ucs2 | 以小端序读取 |
ucs-2 | 以小端序读取 |
utf16le | 以小端序读取 |
utf-16le | 以小端序读取 |
从文件指定位置开始读,如果不指定,则从文件头开始读。读取的范围应该是左闭右开区间 [position, position+length)。有效范围:[0, fileLength - 1]。单位:byte
指定文件的长度,如果不指定,则读到文件末尾。有效范围:[1, fileLength]。单位:byte
文件内容
const fs = qg.getFileSystemManager();
fs.readFile({
filePath: `${qg.env.USER_DATA_PATH}/hello.txt`,
encoding: "utf8",
position: 0,
success(res) {
console.log(res.data);
},
fail(res) {
console.error(res);
}
});
// 同步接口
try {
const res = fs.readFileSync(`${qg.env.USER_DATA_PATH}/hello.txt`, "utf8", 0);
console.log(res);
} catch (e) {
console.error(e);
}
读取目录内文件列表
Object object
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
dirPath | String | 是 | 要读取的目录路径 (本地路径) | |
success | Function | 否 | 接口调用成功的回调函数 | |
fail | Function | 否 | 接口调用失败的回调函数 | |
complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
属性 | 类型 | 说明 |
---|---|---|
files | Array | 指定目录下的文件名数组。 |
注意事项 readdir 接口无法访问文件系统根路径。
FileSystemManager.readdir 的同步版本
要读取的目录路径 (本地路径)
指定目录下的文件名数组。
注意事项 readdir 接口无法访问文件系统根路径。
const fs = qg.getFileSystemManager();
fs.readdir({
dirPath: `${qg.env.USER_DATA_PATH}/example`,
success(res) {
console.log(res.files);
},
fail(res) {
console.error(res);
}
});
// 同步接口
try {
const res = fs.readdirSync(`${qg.env.USER_DATA_PATH}/example`);
console.log(res);
} catch (e) {
console.error(e);
}
读取压缩包内的文件
Object object
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
filePath | String | 是 | 要读取的压缩包的路径 (本地路径) | |
encoding | String | 否 | 统一指定读取文件的字符编码,只在 entries 值为"all"时有效。如果 entries 值为"all"且不传 encoding,则以 ArrayBuffer 格式读取文件的二进制内容 | |
entries | Array | 是 | 要读取的压缩包内的文件列表(当传入"all" 时表示读取压缩包内所有文件) | |
success | Function | 否 | 接口调用成功的回调函数 | |
fail | Function | 否 | 接口调用失败的回调函数 | |
complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
合法值 | 说明 |
---|---|
ascii | |
base64 | |
binary | |
hex | |
utf-8 | |
utf8 | |
latin1 | |
ucs2 | 以小端序读取 |
ucs-2 | 以小端序读取 |
utf16le | 以小端序读取 |
utf-16le | 以小端序读取 |
entries(要读取的压缩包内的文件列表)
结构属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
path | String | 是 | 压缩包内文件路径 | |
encoding | String | 否 | 指定读取文件的字符编码,如果不传 encoding,则以 ArrayBuffer 格式读取文件的二进制内容 | |
position | Number | 否 | 从文件指定位置开始读,如果不指定,则从文件头开始读。读取的范围应该是左闭右开区间 [position, position+length)。有效范围:[0, fileLength - 1]。单位:byte | |
length | Number | 否 | 指定文件的长度,如果不指定,则读到文件末尾。有效范围:[1, fileLength]。单位:byte |
属性 | 类型 | 说明 |
---|---|---|
entries | Object | 文件读取结果。res.entries 是一个对象,key 是文件路径,value 是一个对象 FileItem ,表示该文件的读取结果。每个 FileItem 包含 data (文件内容) 和 errMsg (错误信息) 属性。 |
entries (文件读取结果)
结构属性 | 类型 | 说明 |
---|---|---|
path | Object | 文件路径 |
path(文件路径)
结构属性 | 类型 | 说明 |
---|---|---|
data | String/ArrayBuffer | 文件内容 |
errMsg | String | 错误信息 |
const fs = qg.getFileSystemManager();
// 读取zip内某个或多个文件
fs.readZipEntry({
filePath: "scheme://from/to.zip",
entries: [
{
path: "some_folder/my_file.txt", // zip内文件路径
encoding: "utf-8", // 指定读取文件的字符编码,如果不传 encoding,则以 ArrayBuffer 格式读取文件的二进制内容
position: 0, // 从文件指定位置开始读,如果不指定,则从文件头开始读。读取的范围应该是左闭右开区间 [position, position+length)。有效范围:[0, fileLength - 1]。单位:byte
length: 10000 // 指定文件的长度,如果不指定,则读到文件末尾。有效范围:[1, fileLength]。单位:byte
},
{
path: "other_folder/orther_file.txt" // zip内文件路径
}
],
success(res) {
console.log(res.entries);
},
fail(res) {
console.log(res.errMsg);
}
});
// 读取zip内所有文件。允许指定统一的encoding。position、length则不再允许指定,分别默认为0和文件长度
fs.readZipEntry({
filePath: "scheme://from/to.zip",
entries: "all",
encoding: "utf-8", // 统一指定读取文件的字符编码,如果不传 encoding,则以 ArrayBuffer 格式读取文件的二进制内容
success(res) {
console.log(res.entries);
},
fail(res) {
console.log(res.errMsg);
}
});
读取指定压缩类型的本地文件内容
Object object
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
filePath | String | 是 | 要读取的文件的路径 (本地用户文件或代码包文件) | |
compressionAlgorithm | String | 是 | 文件压缩类型,目前仅支持 'br'。 | |
success | Function | 否 | 接口调用成功的回调函数 | |
fail | Function | 否 | 接口调用失败的回调函数 | |
complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
合法值 | 说明 |
---|---|
br | brotli 压缩文件 |
属性 | 类型 | 说明 |
---|---|---|
data | ArrayBuffer | 文件内容 |
同步读取指定压缩类型的本地文件内容
Object object
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
filePath | String | 是 | 要读取的文件的路径 (本地用户文件或代码包文件) | |
compressionAlgorithm | String | 是 | 文件压缩类型,目前仅支持 'br'。 |
合法值 | 说明 |
---|---|
br | brotli 压缩文件 |
文件读取结果
const fs = qg.getFileSystemManager();
// 异步接口
fs.readCompressedFile({
filePath: "${qg.env.USER_DATA_PATH}/hello.br",
compressionAlgorithm: "br",
success(res) {
console.log(res.data);
},
fail(res) {
console.log("readCompressedFile fail", res);
}
});
// 同步接口
try {
const data = fs.readCompressedFileSync({
filePath: "${qg.env.USER_DATA_PATH}/hello.br",
compressionAlgorithm: "br"
});
console.log(data);
} catch (err) {
console.log(err);
}
复制文件
Object object
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
srcPath | String | 是 | 源文件路径,支持本地路径 | |
destPath | String | 是 | 目标文件路径,支持本地路径 | |
success | Function | 否 | 接口调用成功的回调函数 | |
fail | Function | 否 | 接口调用失败的回调函数 | |
complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
FileSystemManager.copyFile 的同步版本
源文件路径,支持本地路径
目标文件路径,支持本地路径
const fs = qg.getFileSystemManager();
fs.copyFile({
srcPath: `${qg.env.USER_DATA_PATH}/hello.txt`,
destPath: `${qg.env.USER_DATA_PATH}/hello_copy.txt`,
success(res) {
console.log(res);
},
fail(res) {
console.error(res);
}
});
// 同步接口
try {
fs.copyFileSync(
`${qg.env.USER_DATA_PATH}/hello.txt`,
`${qg.env.USER_DATA_PATH}/hello_copy.txt`
);
} catch (e) {
console.error(e);
}
获取该快游戏下的本地临时文件或本地缓存文件信息
Object object
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
filePath | String | 是 | 要读取的文件路径 (本地路径) | |
success | Function | 否 | 接口调用成功的回调函数 | |
fail | Function | 否 | 接口调用失败的回调函数 | |
complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
属性 | 类型 | 说明 |
---|---|---|
size | Number | 文件大小,以字节为单位 |
digest | String | 按照传入的 digestAlgorithm 计算得出的的文件摘要 |
获取该快游戏下已保存的本地缓存文件列表
Object object
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
success | Function | 否 | 接口调用成功的回调函数 | |
fail | Function | 否 | 接口调用失败的回调函数 | |
complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
属性 | 类型 | 说明 |
---|---|---|
fileList | Array | 文件数组 |
fileList(文件数组)
结构属性 | 类型 | 说明 |
---|---|---|
filePath | String | 文件路径 (本地路径) |
size | Number | 本地文件大小,以字节为单位 |
createTime | Number | 文件保存时的时间戳,从 1970/01/01 08:00:00 到当前时间的秒数 |
解压文件
Object object
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
zipFilePath | String | 是 | 源文件路径,支持本地路径,只可以是 zip 压缩文件 | |
targetPath | String | 是 | 目标目录路径,支持本地路径 | |
success | Function | 否 | 接口调用成功的回调函数 | |
fail | Function | 否 | 接口调用失败的回调函数 | |
complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
const fs = qg.getFileSystemManager();
fs.unzip({
zipFilePath: `${qg.env.USER_DATA_PATH}/example.zip`,
targetPath: "${qg.env.USER_DATA_PATH}/example",
success(res) {
console.log(res);
},
fail(res) {
console.error(res);
}
});
判断文件/目录是否存在
Object object
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
path | String | 是 | 要判断是否存在的文件/目录路径 (本地路径) | |
success | Function | 否 | 接口调用成功的回调函数 | |
fail | Function | 否 | 接口调用失败的回调函数 | |
complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
FileSystemManager.access 的同步版本
要判断是否存在的文件/目录路径 (本地路径)
const fs = qg.getFileSystemManager();
// 判断文件/目录是否存在
fs.access({
path: `${qg.env.USER_DATA_PATH}/hello.txt`,
success(res) {
// 文件存在
console.log(res);
},
fail(res) {
// 文件不存在或其他错误
console.error(res);
}
});
// 同步接口
try {
fs.accessSync(`${qg.env.USER_DATA_PATH}/hello.txt`);
} catch (e) {
console.error(e);
}
删除该快游戏下已保存的本地缓存文件
Object object
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
filePath | String | 是 | 需要删除的文件路径 (本地路径) | |
success | Function | 否 | 接口调用成功的回调函数 | |
fail | Function | 否 | 接口调用失败的回调函数 | |
complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
删除目录
Object object
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
dirPath | String | 是 | 要删除的目录路径 (本地路径) | |
recursive | Boolean | false | 否 | 是否递归删除目录。如果为 true,则删除该目录和该目录下的所有子目录以及文件。 |
success | Function | 否 | 接口调用成功的回调函数 | |
fail | Function | 否 | 接口调用失败的回调函数 | |
complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
FileSystemManager.rmdir 的同步版本
要删除的目录路径 (本地路径)
是否递归删除目录。如果为 true,则删除该目录和该目录下的所有子目录以及文件。
const fs = qg.getFileSystemManager();
fs.rmdir({
dirPath: `${qg.env.USER_DATA_PATH}/example`,
recursive: false,
success(res) {
console.log(res);
},
fail(res) {
console.error(res);
}
});
// 同步接口
try {
const res = fs.rmdirSync(`${qg.env.USER_DATA_PATH}/example`, false);
console.log(res);
} catch (e) {
console.error(e);
}
删除文件
Object object
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
filePath | String | 是 | 要删除的文件路径 (本地路径) | |
success | Function | 否 | 接口调用成功的回调函数 | |
fail | Function | 否 | 接口调用失败的回调函数 | |
complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
FileSystemManager.unlink 的同步版本
要删除的文件路径 (本地路径)
const fs = qg.getFileSystemManager();
fs.unlink({
filePath: `${qg.env.USER_DATA_PATH}/hello.txt`,
success(res) {
console.log(res);
},
fail(res) {
console.error(res);
}
});
// 同步接口
try {
const res = fs.unlinkSync(`${qg.env.USER_DATA_PATH}/hello.txt`);
console.log(res);
} catch (e) {
console.error(e);
}
写文件
Object object
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
filePath | String | 是 | 要写入的文件路径 (本地路径) | |
data | String/ArrayBuffer | 是 | 要写入的文本或二进制数据 | |
encoding | String | utf8 | 否 | 指定写入文件的字符编码 |
success | Function | 否 | 用成功的回调函数 | |
fail | Function | 否 | 接口调用失败的回调函数 | |
complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
属性 | 类型 |
---|---|
ascii | |
base64 | |
binary | |
hex | |
utf-8 | |
utf8 | |
latin1 | |
ucs2 | 以小端序读取 |
ucs-2 | 以小端序读取 |
utf16le | 以小端序读取 |
utf-16le | 以小端序读取 |
FileSystemManager.writeFile 的同步版本
要写入的文件路径 (本地路径)
要写入的文本或二进制数据
指定写入文件的字符编码
属性 | 类型 |
---|---|
ascii | |
base64 | |
binary | |
hex | |
utf-8 | |
utf8 | |
latin1 | |
ucs2 | 以小端序读取 |
ucs-2 | 以小端序读取 |
utf16le | 以小端序读取 |
utf-16le | 以小端序读取 |
const fs = qg.getFileSystemManager();
fs.writeFile({
filePath: `${qg.env.USER_DATA_PATH}/hello.txt`,
data: "some text or arrayBuffer",
encoding: "utf8",
success(res) {
console.log(res);
},
fail(res) {
console.error(res);
}
});
// 同步接口
try {
const res = fs.writeFileSync(
`${qg.env.USER_DATA_PATH}/hello.txt`,
"some text or arrayBuffer",
"utf8"
);
console.log(res);
} catch (e) {
console.error(e);
}
在文件结尾追加内容
Object object
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
filePath | String | 是 | 要追加内容的文件路径 (本地路径) | |
data | String/ArrayBuffer | 是 | 要追加的文本或二进制数据 | |
encoding | String | utf8 | 否 | 指定写入文件的字符编码 |
success | Function | 否 | 接口调用成功的回调函数 | |
fail | Function | 否 | 接口调用失败的回调函数 | |
complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
属性 | 类型 |
---|---|
ascii | |
base64 | |
binary | |
hex | |
utf-8 | |
utf8 | |
latin1 | |
ucs2 | 以小端序读取 |
ucs-2 | 以小端序读取 |
utf16le | 以小端序读取 |
utf-16le | 以小端序读取 |
FileSystemManager.appendFile 的同步版本
要追加内容的文件路径 (本地路径)
要追加的文本或二进制数据
指定写入文件的字符编码
属性 | 类型 |
---|---|
ascii | |
base64 | |
binary | |
hex | |
utf-8 | |
utf8 | |
latin1 | |
ucs2 | 以小端序读取 |
ucs-2 | 以小端序读取 |
utf16le | 以小端序读取 |
utf-16le | 以小端序读取 |
const fs = qg.getFileSystemManager();
fs.appendFile({
filePath: `${qg.env.USER_DATA_PATH}/hello.txt`,
data: "some text",
encoding: "utf8",
success(res) {
console.log(res);
},
fail(res) {
console.error(res);
}
});
// 同步接口
try {
fs.appendFileSync(`${qg.env.USER_DATA_PATH}/hello.txt`, "some text", "utf8");
} catch (e) {
console.error(e);
}
重命名文件。可以把文件从 oldPath 移动到 newPath
Object object
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
oldPath | String | 是 | 源文件路径,支持本地路径 | |
newPath | String | 是 | 新文件路径,支持本地路径 | |
success | Function | 否 | 接口调用成功的回调函数 | |
fail | Function | 否 | 接口调用失败的回调函数 | |
complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
FileSystemManager.rename 的同步版本
源文件路径,支持本地路径
新文件路径,支持本地路径
const fs = qg.getFileSystemManager();
fs.rename({
oldPath: `${qg.env.USER_DATA_PATH}/hello.txt`,
newPath: `${qg.env.USER_DATA_PATH}/hello_new.txt`,
success(res) {
console.log(res);
},
fail(res) {
console.error(res);
}
});
// 同步接口
try {
const res = fs.renameSync(
`${qg.env.USER_DATA_PATH}/hello.txt`,
`${qg.env.USER_DATA_PATH}/hello_new.txt`
);
console.log(res);
} catch (e) {
console.error(e);
}
获取文件 Stats 对象
Object object
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
path | String | 是 | 文件/目录路径 (本地路径) | |
recursive | Boolean | false | 否 | 是否递归获取目录下的每个文件的 Stats 信息 |
success | Function | 否 | 接口调用成功的回调函数 | |
fail | Function | 否 | 接口调用失败的回调函数 | |
complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
属性 | 类型 | 说明 |
---|---|---|
stats | Stats/Array | 当 recursive 为 false 时,res.stats 是一个 Stats 对象。当 recursive 为 true 且 path 是一个目录的路径时,res.stats 是一个 Array,数组的每一项是一个对象,每个对象包含 path 和 stats。 |
recursive 为 false 时
// 异步版本
let fs = qg.getFileSystemManager();
fs.stat({
path: `${qg.env.USER_DATA_PATH}/testDir`,
success: res => {
console.log(res.stats.isDirectory());
}
});
// 同步版本
fs.statSync(`${qg.env.USER_DATA_PATH}/testDir`, false);
recursive 为 true 时
let fs = qg.getFileSystemManager();
// 异步版本
fs.stat({
path: `${qg.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());
});
}
});
// 同步版本
fs.statSync(`${qg.env.USER_DATA_PATH}/testDir`, true);
FileSystemManager.stat 的同步版本
文件/目录路径 (本地路径)
是否递归获取目录下的每个文件的 Stats 信息
当 recursive 为 false 时,res.stats 是一个 Stats 对象。当 recursive 为 true 且 path 是一个目录的路径时,res.stats 是一个 Array,数组的每一项是一个对象,每个对象包含 path 和 stats。
recursive 为 false 时
// 异步版本
let fs = qg.getFileSystemManager();
fs.stat({
path: `${qg.env.USER_DATA_PATH}/testDir`,
success: res => {
console.log(res.stats.isDirectory());
}
});
// 同步版本
fs.statSync(`${qg.env.USER_DATA_PATH}/testDir`, false);
recursive 为 true 时
let fs = qg.getFileSystemManager();
// 异步版本
fs.stat({
path: `${qg.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());
});
}
});
// 同步版本
fs.statSync(`${qg.env.USER_DATA_PATH}/testDir`, true);
描述文件状态的对象
文件的类型和存取的权限,对应 POSIX stat.st_mode
文件大小,单位:B,对应 POSIX stat.st_size
文件最近一次被存取或被执行的时间,UNIX 时间戳,对应 POSIX stat.st_atime
文件最后一次被修改的时间,UNIX 时间戳,对应 POSIX stat.st_mtime
判断当前文件是否一个目录
表示当前文件是否一个目录
判断当前文件是否一个普通文件
表示当前文件是否一个普通文件