文档管理中心
我的
您当前正在浏览新版开发者文档中心,目录分类和层级有所调整。点击左侧当前文档分类名称前的“☰”图标,可切换文档分类。 了解新版目录

打开文件

场景介绍

普通应用无法直接访问公共路径下的文件,Enterprise Data Guard Kit为应用提供相关接口以获取文件描述符(fd)。

接口说明

详细接口说明可参考接口文档。

展开
接口名 描述
openFile(path: string, callback: AsyncCallback<number>): void 通过Callback方式打开指定路径下的文件,获取文件描述符(fd)。
openFile(path: string): Promise<number> 使用Promise方式打开指定路径下的文件,获取文件描述符(fd)。
openFileWrite(path: string, callback: AsyncCallback<number>): void 在只写模式下,通过Callback方式打开用户个人数据目录下的文件,获取文件描述符(fd)。
openFileWrite(path: string): Promise<number> 在只写模式下,使用Promise方式打开用户个人数据目录下的文件,获取文件描述符(fd)。

开发步骤

  1. 导入模块。

    收起
    自动换行
    深色代码主题
    复制
    1. import { BusinessError } from '@kit.BasicServicesKit';
    2. import { fileGuard } from '@kit.EnterpriseDataGuardKit';
    3. import { hilog } from '@kit.PerformanceAnalysisKit';
  2. 初始化FileGuard对象guard,调用接口openFile,并且可选择以下一种方式打开文件,获取指定目录文件fd。

    • 通过回调函数方式,打开指定路径下的文件,获取文件fd。

      收起
      自动换行
      深色代码主题
      复制
      1. const TAG: string = 'FileGuard_OpenFile';
      2. const DOMAIN: number = 0x0000;
      3. /**
      4. * 打开文件。使用callback异步回调。
      5. */
      6. function openFileCallback() {
      7. let guard: fileGuard.FileGuard = new fileGuard.FileGuard();
      8. let path: string = '/data/service/el2/test/test1.txt';
      9. guard.openFile(path, (err: BusinessError, fd: number) => {
      10. if (err) {
      11. hilog.error(DOMAIN, TAG, `Failed to open file. Code: ${err.code}, message: ${err.message}.`);
      12. return;
      13. }
      14. hilog.info(DOMAIN, TAG, `Succeeded in opening file. path: ${path}, fd: ${fd}.`);
      15. });
      16. }
    • 通过Promise方式,打开指定路径下的文件,获取文件fd。

      收起
      自动换行
      深色代码主题
      复制
      1. const TAG: string = 'FileGuard_OpenFile';
      2. const DOMAIN: number = 0x0000;
      3. // ...
      4. /**
      5. * 打开文件。使用Promise异步回调。
      6. */
      7. function openFilePromise() {
      8. let guard: fileGuard.FileGuard = new fileGuard.FileGuard();
      9. let path: string = '/data/service/el2/test/test2.txt';
      10. guard.openFile(path).then((fd: number) => {
      11. hilog.info(DOMAIN, TAG, `Succeeded in opening file. path: ${path}, fd: ${fd}.`);
      12. }).catch((err: BusinessError) => {
      13. hilog.error(DOMAIN, TAG, `Failed to open file. Code: ${err.code}, message: ${err.message}.`);
      14. });
      15. }
  3. 只写模式下,打开用户个人数据目录下的文件,获取文件描述符。初始化FileGuard对象guard,调用接口openFileWrite,并且可选择以下一种方式获取指定目录文件fd。

    • 通过回调函数方式,打开用户个人数据目录下的文件,获取文件fd。

      收起
      自动换行
      深色代码主题
      复制
      1. const TAG: string = 'FileGuard_OpenFile';
      2. const DOMAIN: number = 0x0000;
      3. // ...
      4. /**
      5. * 只写模式打开文件。使用callback异步回调。
      6. * @param accountId: 用户ID
      7. */
      8. function openFileWriteCallback(accountId: number) {
      9. let guard: fileGuard.FileGuard = new fileGuard.FileGuard();
      10. let path: string = `/data/service/el2/${accountId}/hmdfs/account/files/Docs/Documents/aaa.txt`;
      11. guard.openFileWrite(path, (err: BusinessError, fd: number) => {
      12. if (err) {
      13. hilog.error(DOMAIN, TAG, `Failed to open file in write-only mode. Code: ${err.code}, message: ${err.message}.`);
      14. return;
      15. }
      16. hilog.info(DOMAIN, TAG, `Succeeded in opening file in write-only mode. path: ${path}, fd: ${fd}.`);
      17. });
      18. }
    • 通过Promise方式,打开用户个人数据目录下的文件,获取文件fd。

      收起
      自动换行
      深色代码主题
      复制
      1. const TAG: string = 'FileGuard_OpenFile';
      2. const DOMAIN: number = 0x0000;
      3. // ...
      4. /**
      5. * 只写模式打开文件。使用Promise异步回调。
      6. * @param accountId: 用户ID
      7. */
      8. function openFileWritePromise(accountId: number) {
      9. let guard: fileGuard.FileGuard = new fileGuard.FileGuard();
      10. let path: string = `/data/service/el2/${accountId}/hmdfs/account/files/Docs/Documents/aaa.txt`;
      11. guard.openFileWrite(path).then((fd: number) => {
      12. hilog.info(DOMAIN, TAG, `Succeeded in opening file in write-only mode. path: ${path}, fd: ${fd}.`);
      13. }).catch((err: BusinessError) => {
      14. hilog.error(DOMAIN, TAG, `Failed to open file in write-only mode. Code: ${err.code}, message: ${err.message}.`);
      15. });
      16. }