Intelligent Assistant
Chat with our virtual assistant to get answers promptly.
We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.
HarmonyOS
Common apps cannot directly access files at a common path. To address this problem, Enterprise Data Guard Kit provides APIs for apps to obtain the file descriptor (FD) of a file for opening it.
For details about these APIs, please refer to the API Reference.
| API | Description |
|---|---|
| openFile(path: string, callback: AsyncCallback<number>): void | Opens a file in a specified path and obtains the FD using a callback. |
| openFile(path: string): Promise<number> | Opens a file in a specified path and obtains the FD using a promise. |
| openFileWrite(path: string, callback: AsyncCallback<number>): void | Opens a file in the personal data directory of a user and obtains the FD in write-only mode using a callback. |
| openFileWrite(path: string): Promise<number> | Opens a file in the personal data directory of a user and obtains the FD in write-only mode using a promise. |
Import required modules.
- import { BusinessError } from '@kit.BasicServicesKit';
- import { fileGuard } from '@kit.EnterpriseDataGuardKit';
- import { hilog } from '@kit.PerformanceAnalysisKit';
Initialize the guard object of FileGuard, call the openFile API, and use either of the following methods to open a file and obtain the FD of the file in the specified directory:
Open a file in a specified path and obtain the FD using a callback.
- const TAG: string = 'FileGuard_OpenFile';
- const DOMAIN: number = 0x0000;
-
- /**
- * Open a file and return the result asynchronously using a callback.
- */
- function openFileCallback() {
- let guard: fileGuard.FileGuard = new fileGuard.FileGuard();
- let path: string = '/data/service/el2/test/test1.txt';
- guard.openFile(path, (err: BusinessError, fd: number) => {
- if (err) {
- hilog.error(DOMAIN, TAG, `Failed to open file. Code: ${err.code}, message: ${err.message}.`);
- return;
- }
- hilog.info(DOMAIN, TAG, `Succeeded in opening file. path: ${path}, fd: ${fd}.`);
- });
- }
Open a file in a specified path and obtain the FD using a promise.
- const TAG: string = 'FileGuard_OpenFile';
- const DOMAIN: number = 0x0000;
-
- // ...
- /**
- * Open a file and return the result asynchronously using a promise.
- */
- function openFilePromise() {
- let guard: fileGuard.FileGuard = new fileGuard.FileGuard();
- let path: string = '/data/service/el2/test/test2.txt';
- guard.openFile(path).then((fd: number) => {
- hilog.info(DOMAIN, TAG, `Succeeded in opening file. path: ${path}, fd: ${fd}.`);
- }).catch((err: BusinessError) => {
- hilog.error(DOMAIN, TAG, `Failed to open file. Code: ${err.code}, message: ${err.message}.`);
- });
- }
Open a file in the personal data directory of a user and obtain the file descriptor in write-only mode. Initialize the guard object of FileGuard, call the openFileWrite API, and use either of the following methods obtain the FD of the file in the specified directory:
Open a file in the personal data directory of a user and obtain the FD using a callback.
- const TAG: string = 'FileGuard_OpenFile';
- const DOMAIN: number = 0x0000;
-
- // ...
- /**
- * Open a file in write-only mode and return the result asynchronously using a callback.
- * @param accountId: user ID
- */
- function openFileWriteCallback(accountId: number) {
- let guard: fileGuard.FileGuard = new fileGuard.FileGuard();
- let path: string = `/data/service/el2/${accountId}/hmdfs/account/files/Docs/Documents/aaa.txt`;
- guard.openFileWrite(path, (err: BusinessError, fd: number) => {
- if (err) {
- hilog.error(DOMAIN, TAG, `Failed to open file in write-only mode. Code: ${err.code}, message: ${err.message}.`);
- return;
- }
- hilog.info(DOMAIN, TAG, `Succeeded in opening file in write-only mode. path: ${path}, fd: ${fd}.`);
- });
- }
Open a file in the personal data directory of a user and obtain the FD using a promise.
- const TAG: string = 'FileGuard_OpenFile';
- const DOMAIN: number = 0x0000;
-
- // ...
- /**
- * Open a file in write-only mode and return the result asynchronously using a promise.
- * @param accountId: user ID
- */
- function openFileWritePromise(accountId: number) {
- let guard: fileGuard.FileGuard = new fileGuard.FileGuard();
- let path: string = `/data/service/el2/${accountId}/hmdfs/account/files/Docs/Documents/aaa.txt`;
- guard.openFileWrite(path).then((fd: number) => {
- hilog.info(DOMAIN, TAG, `Succeeded in opening file in write-only mode. path: ${path}, fd: ${fd}.`);
- }).catch((err: BusinessError) => {
- hilog.error(DOMAIN, TAG, `Failed to open file in write-only mode. Code: ${err.code}, message: ${err.message}.`);
- });
- }
Intelligent Assistant
Chat with our virtual assistant to get answers promptly.
Quick start
Helps you find desired resources with ease.