HarmonyOS 鸿蒙Next 怎么获取/storage/hitrace 文件夹下的所有文件名
HarmonyOS 鸿蒙Next 怎么获取/storage/hitrace 文件夹下的所有文件名
通过hdc shell 方式 在/storage 下创建了hitrace 文件夹,然后在hitrace 目录下 添加了一些文件。
问题:
- 在ts 端 通过什么接口,能够获取上述hitrace 目录下的所有文件名?
- napi 方式,open_dir("/storage/hitrace/") 返回 nullptr errno 为2,是否需要权限?若有,咋设?
更多关于HarmonyOS 鸿蒙Next 怎么获取/storage/hitrace 文件夹下的所有文件名的实战教程也可以访问 https://www.itying.com/category-93-b0.html
3 回复
不支持去获取自定义目录的文件名
更多关于HarmonyOS 鸿蒙Next 怎么获取/storage/hitrace 文件夹下的所有文件名的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
好用,mark,
在HarmonyOS(鸿蒙Next)中,获取/storage/hitrace
文件夹下的所有文件名,可以使用File
类进行文件操作。具体步骤如下:
-
引入必要的模块:
import fileio from '[@ohos](/user/ohos).fileio'; import featureAbility from '[@ohos](/user/ohos).ability.featureAbility';
-
获取文件目录的句柄:
const context = featureAbility.getContext(); const dirPath = '/storage/hitrace'; const dir = await context.getFilesDir();
-
读取文件夹下的所有文件名:
const fileList = await fileio.listFile(dirPath);
-
输出文件名:
for (let file of fileList) { console.log(file); }
完整代码如下:
import fileio from '[@ohos](/user/ohos).fileio';
import featureAbility from '[@ohos](/user/ohos).ability.featureAbility';
async function getFileNames() {
const context = featureAbility.getContext();
const dirPath = '/storage/hitrace';
const fileList = await fileio.listFile(dirPath);
for (let file of fileList) {
console.log(file);
}
}
getFileNames();
这段代码会输出/storage/hitrace
文件夹下的所有文件名。