1# 应用文件访问 2 3应用需要对应用文件目录下的应用文件进行查看、创建、读写、删除、移动、复制、获取属性等访问操作,下文介绍具体方法。 4 5## 接口说明 6 7开发者通过基础文件操作接口([ohos.file.fs](../reference/apis-core-file-kit/js-apis-file-fs.md))实现应用文件访问能力,主要功能如下表所示。 8 9**表1** 基础文件操作接口功能 10 11| 接口名 | 功能 | 接口类型 | 支持同步 | 支持异步 | 12| -------- | -------- | -------- | -------- | -------- | 13| access | 检查文件是否存在 | 方法 | √ | √ | 14| close | 关闭文件 | 方法 | √ | √ | 15| copyFile | 复制文件 | 方法 | √ | √ | 16| createStream | 基于文件路径打开文件流 | 方法 | √ | √ | 17| listFile | 列出文件夹下所有文件名 | 方法 | √ | √ | 18| mkdir | 创建目录 | 方法 | √ | √ | 19| moveFile | 移动文件 | 方法 | √ | √ | 20| open | 打开文件 | 方法 | √ | √ | 21| read | 从文件读取数据 | 方法 | √ | √ | 22| rename | 重命名文件或文件夹 | 方法 | √ | √ | 23| rmdir | 删除整个目录 | 方法 | √ | √ | 24| stat | 获取文件详细属性信息 | 方法 | √ | √ | 25| unlink | 删除单个文件 | 方法 | √ | √ | 26| write | 将数据写入文件 | 方法 | √ | √ | 27| Stream.close | 关闭文件流 | 方法 | √ | √ | 28| Stream.flush | 刷新文件流 | 方法 | √ | √ | 29| Stream.write | 将数据写入流文件 | 方法 | √ | √ | 30| Stream.read | 从流文件读取数据 | 方法 | √ | √ | 31| File.fd | 获取文件描述符 | 属性 | - | - | 32| OpenMode | 设置文件打开标签 | 属性 | - | - | 33| Filter | 设置文件过滤配置项 | 类型 | - | - | 34 35## 开发示例 36 37在对应用文件开始访问前,开发者需要[获取应用文件路径](../application-models/application-context-stage.md#获取应用文件路径)。以从UIAbilityContext获取HAP级别的文件路径为例进行说明,UIAbilityContext的获取方式请参见[获取UIAbility的上下文信息](../application-models/uiability-usage.md#获取uiability的上下文信息)。 38 39下面介绍几种常用操作示例。 40 41### 新建并读写一个文件 42 43以下示例代码演示了如何新建一个文件并对其读写。 44 45```ts 46// pages/xxx.ets 47import fs, { ReadOptions } from '@ohos.file.fs'; 48import common from '@ohos.app.ability.common'; 49import buffer from '@ohos.buffer'; 50 51// 获取应用文件路径 52let context = getContext(this) as common.UIAbilityContext; 53let filesDir = context.filesDir; 54 55function createFile(): void { 56 // 新建并打开文件 57 let file = fs.openSync(filesDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 58 // 写入一段内容至文件 59 let writeLen = fs.writeSync(file.fd, "Try to write str."); 60 console.info("The length of str is: " + writeLen); 61 // 从文件读取一段内容 62 let arrayBuffer = new ArrayBuffer(1024); 63 let readOptions: ReadOptions = { 64 offset: 0, 65 length: arrayBuffer.byteLength 66 }; 67 let readLen = fs.readSync(file.fd, arrayBuffer, readOptions); 68 let buf = buffer.from(arrayBuffer, 0, readLen); 69 console.info("the content of file: " + buf.toString()); 70 // 关闭文件 71 fs.closeSync(file); 72} 73``` 74 75### 读取文件内容并写入到另一个文件 76 77以下示例代码演示了如何从一个文件读写内容到另一个文件。 78 79```ts 80// pages/xxx.ets 81import fs, { ReadOptions } from '@ohos.file.fs'; 82import common from '@ohos.app.ability.common'; 83 84// 获取应用文件路径 85let context = getContext(this) as common.UIAbilityContext; 86let filesDir = context.filesDir; 87 88function readWriteFile(): void { 89 // 打开文件 90 let srcFile = fs.openSync(filesDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 91 let destFile = fs.openSync(filesDir + '/destFile.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 92 // 读取源文件内容并写入至目的文件 93 let bufSize = 4096; 94 let readSize = 0; 95 let buf = new ArrayBuffer(bufSize); 96 let readOptions: ReadOptions = { 97 offset: readSize, 98 length: bufSize 99 }; 100 let readLen = fs.readSync(srcFile.fd, buf, readOptions); 101 while (readLen > 0) { 102 readSize += readLen; 103 fs.writeSync(destFile.fd, buf); 104 readOptions.offset = readSize; 105 readLen = fs.readSync(srcFile.fd, buf, readOptions); 106 } 107 // 关闭文件 108 fs.closeSync(srcFile); 109 fs.closeSync(destFile); 110} 111``` 112 113> **说明:** 114> 115> 使用读写接口时,需注意可选项参数offset的设置。对于已存在且读写过的文件,文件偏移指针默认在上次读写操作的终止位置。 116 117### 以流的形式读写文件 118 119以下示例代码演示了如何使用流接口进行文件读写: 120 121```ts 122// pages/xxx.ets 123import fs, { ReadOptions } from '@ohos.file.fs'; 124import common from '@ohos.app.ability.common'; 125 126// 获取应用文件路径 127let context = getContext(this) as common.UIAbilityContext; 128let filesDir = context.filesDir; 129 130async function readWriteFileWithStream(): Promise<void> { 131 // 打开文件流 132 let inputStream = fs.createStreamSync(filesDir + '/test.txt', 'r+'); 133 let outputStream = fs.createStreamSync(filesDir + '/destFile.txt', "w+"); 134 // 以流的形式读取源文件内容并写入目的文件 135 let bufSize = 4096; 136 let readSize = 0; 137 let buf = new ArrayBuffer(bufSize); 138 let readOptions: ReadOptions = { 139 offset: readSize, 140 length: bufSize 141 }; 142 let readLen = await inputStream.read(buf, readOptions); 143 readSize += readLen; 144 while (readLen > 0) { 145 await outputStream.write(buf); 146 readOptions.offset = readSize; 147 readLen = await inputStream.read(buf, readOptions); 148 readSize += readLen; 149 } 150 // 关闭文件流 151 inputStream.closeSync(); 152 outputStream.closeSync(); 153} 154``` 155 156> **说明:** 157> 158> 使用流接口时,需注意流的及时关闭。同时流的异步接口应严格遵循异步接口使用规范,避免同步、异步接口混用。流接口不支持并发读写。 159 160### 查看文件列表 161 162以下示例代码演示了如何查看文件列表: 163 164```ts 165import fs, { Filter, ListFileOptions } from '@ohos.file.fs'; 166import common from '@ohos.app.ability.common'; 167 168// 获取应用文件路径 169let context = getContext(this) as common.UIAbilityContext; 170let filesDir = context.filesDir; 171 172// 查看文件列表 173function getListFile(): void { 174 let listFileOption: ListFileOptions = { 175 recursion: false, 176 listNum: 0, 177 filter: { 178 suffix: [".png", ".jpg", ".txt"], 179 displayName: ["test*"], 180 fileSizeOver: 0, 181 lastModifiedAfter: new Date(0).getTime() 182 } 183 }; 184 let files = fs.listFileSync(filesDir, listFileOption); 185 for (let i = 0; i < files.length; i++) { 186 console.info(`The name of file: ${files[i]}`); 187 } 188} 189``` 190