遍历目录中所有文件,遍历指定目录,获取当前目录下所有文件,并通过页面展示文件列表。

遍历目录中所有文件


HarmonyOS
2024-05-20 22:11:21
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
put_get

在应用file目录下,手动创建文件和目录类型的两种文件用于测试,然后使用fileio模块对file目录进行遍历,获取当前目录下所有文件,并通过List组件进行显示。

能力介绍

模块:fileio文件管理

示例实现

获取file目录沙箱路径。

let path = getContext(this).filesDir
  • 1.

file目录下新建4个测试文件。

let fd1 = fileio.openSync(path + '/testFile_01', 0o102, 0o640); 
let fd2 = fileio.openSync(path + '/testFile_02', 0o102, 0o640); 
fileio.mkdirSync(path + '/testDir_01', 0o775) 
fileio.mkdirSync(path + '/testDir_02', 0o775)
  • 1.
  • 2.
  • 3.
  • 4.

打开当前应用的file目录,获取dir对象。

let dir = fileio.opendirSync(path)
  • 1.

遍历目录,将目录中文件路径存入数组。

// 读取下一个目录项 
let dirent = dir.readSync() 
while (dirent) { 
let name = dirent.name 
this.pathList.push(name) 
dirent = dir.readSync() 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

使用List组件展示文件路径列表。

if (this.pathList.length > 0) { 
List() { 
ForEach(this.pathList, (path) => { 
ListItem() { 
Column() { 
Row() { 
Text(path) 
.fontSize(20) 
}.height(30) 
}.width("100%") 
} 
}) 
} 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

全量代码

import fileio from '@ohos.fileio'; 
import prompt from '@ohos.prompt'; 
  
  
@Entry 
@Component 
struct DirPage { 
@State message: string = 'Hello World' 
@State pathList: Array<string> = [] 
  
  
build() { 
Row() { 
Column({ space: 8 }) { 
Button(`创建文件`) 
.fontSize(30) 
.onClick(() => { 
let path = getContext(this).filesDir 
let fd1 = fileio.openSync(path + '/testFile_01', 0o102, 0o640); 
let fd2 = fileio.openSync(path + '/testFile_02', 0o102, 0o640); 
fileio.close(fd1) 
fileio.close(fd2) 
prompt.showToast({message:`创建文件成功`}) 
}) 
Button(`创建目录`) 
.fontSize(30) 
.onClick(() => { 
let path = getContext(this).filesDir 
fileio.mkdirSync(path + '/testDir_01', 0o775) 
fileio.mkdirSync(path + '/testDir_02', 0o775) 
prompt.showToast({message:`创建目录成功`}) 
}) 
Button(`获取所有文件`) 
.fontSize(30) 
.onClick(() => { 
let path = getContext(this).filesDir 
// 打开目录 
let dir = fileio.opendirSync(path) 
  
  
// 读取下一个目录项 
let dirent = dir.readSync() 
while (dirent) { 
let name = dirent.name 
  
  
this.pathList.push(name) 
dirent = dir.readSync() 
} 
dir.close() 
prompt.showToast({message:`获取所有文件成功`}) 
}) 
if (this.pathList.length > 0) { 
List() { 
ForEach(this.pathList, (path) => { 
ListItem() { 
Column() { 
Row() { 
Text(path) 
.fontSize(20) 
}.height(30) 
}.width("100%") 
} 
}) 
} 
} 
} 
.width('100%') 
} 
.height('100%') 
} 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
分享
微博
QQ
微信
回复
2024-05-22 16:00:54


相关问题
如何获取工程目录下的json文件
1704浏览 • 1回复 待解决
HarmonyOS 下载文件保存到指定目录
1158浏览 • 1回复 待解决
HarmonyOS 如何遍历resources/rawfile目录
569浏览 • 1回复 待解决
java怎么读取公共目录下文件
3131浏览 • 1回复 待解决
鸿蒙如何读取resources目录下文件
4473浏览 • 1回复 待解决