提交 0e1e9ce9 编写于 作者: A Annie_wang

update docs

Signed-off-by: NAnnie_wang <annie.wangli@huawei.com>
上级 8e2fcf0d
# User File Access and Management # @ohos.data.fileAccess
The **fileAccess** module is a framework for accessing and operating user files based on the extension mechanism. This module interacts with diverse file management services, such as the media library and external storage management service, and provides a set of unified file access and management APIs for system applications. The media library service allows access to user files on local devices and distributed devices. The external storage management service allows access to the user files stored on devices such as shared disks, USB flash drives, and SD cards. The **fileAccess** module is a framework for accessing and operating user files based on the Extension ability mechanism. This module interacts with diverse file management services, such as the media library and external storage management service, and provides a set of file access and management APIs for system applications. The media library service allows access to user files on local devices and distributed devices. The external storage management service allows access to the user files stored on devices such as shared disks, USB flash drives, and SD cards.
>**NOTE** >**NOTE**
> >
>- The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version. >- The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
>- The APIs provided by this module are system APIs and cannot be called by third-party applications. Currently, the APIs can be called only by FilePicker and the file manager app. >- The APIs provided by this module are system APIs and cannot be called by third-party applications. Currently, the APIs can be called only by **FilePicker** and **Files**.
## Modules to Import ## Modules to Import
...@@ -21,17 +21,13 @@ Obtains information about all wants with **extension** set to **fileAcesss** in ...@@ -21,17 +21,13 @@ Obtains information about all wants with **extension** set to **fileAcesss** in
**System capability**: SystemCapability.FileManagement.UserFileService **System capability**: SystemCapability.FileManagement.UserFileService
**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER **Required permissions**: ohos.permission.FILE_ACCESS_MANAGER and ohos.permission.GET_BUNDLE_INFO_PRIVILEGED
**Parameters**
None
**Return value** **Return value**
| Type| Description| | Type| Description|
| --- | -- | | --- | -- |
| Promise&lt;Array&lt;Want&gt;&gt; | Promise used to return the want information obtained.| | Promise&lt;Array&lt;Want&gt;&gt; | Promise used to return the **want** information obtained.|
**Example** **Example**
...@@ -55,27 +51,27 @@ Synchronously creates a **Helper** object to connect to the specified wants. The ...@@ -55,27 +51,27 @@ Synchronously creates a **Helper** object to connect to the specified wants. The
**System capability**: SystemCapability.FileManagement.UserFileService **System capability**: SystemCapability.FileManagement.UserFileService
**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER **Required permissions**: ohos.permission.FILE_ACCESS_MANAGER and ohos.permission.GET_BUNDLE_INFO_PRIVILEGED
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| --- | --- | --- | -- | | --- | --- | --- | -- |
| context | Context | Yes| Context of the ability.| | context | Context | Yes| Context of the ability.|
| wants | Array&lt;Want&gt; | Yes| Wants to connect.| | wants | Array&lt;Want&gt; | Yes| Wants to connect.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| --- | -- | | --- | -- |
| FileAccessHelper | **Helper** object created.| | FileAccessHelper | **Helper** object created.|
**Example** **Example**
```js ```js
createFileAccessHelper() { createFileAccessHelper() {
let fileAccesssHelper = null; let fileAccessHelper = null;
// Obtain want information by using getFileAccessAbilityInfo(). / / Obtain wantInfos by using getFileAccessAbilityInfo().
// Create a Helper object to interact with the media library service only. // Create a Helper object to interact with the media library service only.
let wantInfos = [ let wantInfos = [
{ {
...@@ -84,8 +80,9 @@ Synchronously creates a **Helper** object to connect to the specified wants. The ...@@ -84,8 +80,9 @@ Synchronously creates a **Helper** object to connect to the specified wants. The
}, },
] ]
try { try {
fileAccesssHelper = fileAccess.createFileAccessHelper(this.context, wantInfos); // This.context is passed by MainAbility.
if (!fileAccesssHelper) fileAccessHelper = fileAccess.createFileAccessHelper(this.context, wantInfos);
if (!fileAccessHelper)
console.error("createFileAccessHelper interface returns an undefined object"); console.error("createFileAccessHelper interface returns an undefined object");
} catch (error) { } catch (error) {
console.error("createFileAccessHelper failed, error " + error); console.error("createFileAccessHelper failed, error " + error);
...@@ -97,23 +94,23 @@ Synchronously creates a **Helper** object to connect to the specified wants. The ...@@ -97,23 +94,23 @@ Synchronously creates a **Helper** object to connect to the specified wants. The
createFileAccessHelper(context: Context) : FileAccessHelper createFileAccessHelper(context: Context) : FileAccessHelper
Synchronously creates a **Helper** object that connects to all file management services in the current system. Synchronously creates a **Helper** object to connect to all file management services in the system.
**System capability**: SystemCapability.FileManagement.UserFileService **System capability**: SystemCapability.FileManagement.UserFileService
**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER **Required permissions**: ohos.permission.FILE_ACCESS_MANAGER and ohos.permission.GET_BUNDLE_INFO_PRIVILEGED
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| --- | --- | --- | -- | | --- | --- | --- | -- |
| context | Context | Yes| Context of the ability.| | context | Context | Yes| Context of the ability.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| --- | -- | | --- | -- |
| FileAccessHelper | **Helper** object created.| | FileAccessHelper | **Helper** object created.|
**Example** **Example**
...@@ -122,6 +119,7 @@ Synchronously creates a **Helper** object that connects to all file management s ...@@ -122,6 +119,7 @@ Synchronously creates a **Helper** object that connects to all file management s
let fileAccesssHelperAllServer = null; let fileAccesssHelperAllServer = null;
// Create a Helper object to interact with all file management services configured with fileAccess in the system. // Create a Helper object to interact with all file management services configured with fileAccess in the system.
try { try {
// This.context is passed by MainAbility.
fileAccesssHelperAllServer = fileAccess.createFileAccessHelper(this.context); fileAccesssHelperAllServer = fileAccess.createFileAccessHelper(this.context);
if (!fileAccesssHelperAllServer) if (!fileAccesssHelperAllServer)
console.error("createFileAccessHelper interface returns an undefined object"); console.error("createFileAccessHelper interface returns an undefined object");
...@@ -135,22 +133,18 @@ Synchronously creates a **Helper** object that connects to all file management s ...@@ -135,22 +133,18 @@ Synchronously creates a **Helper** object that connects to all file management s
getRoots( ) : Promise&lt;RootIterator&gt; getRoots( ) : Promise&lt;RootIterator&gt;
Obtains information about the root nodes for the devices of the file management service type connected to the **Helper** object. This API uses a promise to return a **RootIterator** object, which Obtains information about the device root nodes of the file management service type connected to the **Helper** object. This API uses a promise to return a **RootIterator** object, which
returns [RootInfo](#rootinfo) by using [next()](#rootiteratornext). returns [RootInfo](#rootinfo) by using [next()](#rootiteratornext).
**System capability**: SystemCapability.FileManagement.UserFileService **System capability**: SystemCapability.FileManagement.UserFileService
**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER **Required permissions**: ohos.permission.FILE_ACCESS_MANAGER
**Parameters**
None
**Return value** **Return value**
| Type| Description| | Type| Description|
| --- | -- | | --- | -- |
| Promise&lt;RootIterator&gt; | **RootIterator** object obtained.| | Promise&lt;RootIterator&gt; | Promise used to return the **RootIterator** object obtained.|
**Example** **Example**
...@@ -160,7 +154,8 @@ None ...@@ -160,7 +154,8 @@ None
let rootinfos = []; let rootinfos = [];
let isDone = false; let isDone = false;
try { try {
rootIterator = await fileAccesssHelper.getRoots(); // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper.
rootIterator = await fileAccessHelper.getRoots();
if (!rootIterator) { if (!rootIterator) {
console.error("getRoots interface returns an undefined object"); console.error("getRoots interface returns an undefined object");
return; return;
...@@ -182,7 +177,7 @@ None ...@@ -182,7 +177,7 @@ None
listFile(filter?: Filter) : FileIterator listFile(filter?: Filter) : FileIterator
Synchronously obtains the **FileIterator** object of the first-level files (file folder) matching the conditions of the filter from the root node of a device. The **FileIterator** object then returns [FileInfo](#fileinfo) by using [next()](#fileiteratornext). Synchronously obtains the **FileIterator** object of the first-level files (file folder) matching the conditions of the filter from the device root node. The **FileIterator** object then returns [FileInfo](#fileinfo) by using [next()](#fileiteratornext).
**System capability**: SystemCapability.FileManagement.UserFileService **System capability**: SystemCapability.FileManagement.UserFileService
...@@ -190,28 +185,28 @@ Synchronously obtains the **FileIterator** object of the first-level files (file ...@@ -190,28 +185,28 @@ Synchronously obtains the **FileIterator** object of the first-level files (file
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| --- | --- | -- | -- | | --- | --- | -- | -- |
| filter | Filter | No| **Filter** object. | | filter | Filter | No| **Filter** object. |
**Return value** **Return value**
| Type| Description| | Type| Description|
| --- | -- | | --- | -- |
| FileIterator | **FileIterator** object obtained.| | FileIterator | **FileIterator** object obtained.|
**Example** **Example**
```js ```js
/ / Obtain root information by using getRoots(). / / Obtain rootInfos by using getRoots().
// let filter = {suffix : [".txt", ".jpg", ".xlsx"]}; // let filter = {suffix : [".txt", ".jpg", ".xlsx"]};
let rootInfo = rootinfos[0]; let rootInfo = rootinfos[0];
let fileInfos = []; let fileInfos = [];
let isDone = false; let isDone = false;
try { try {
let fileIterator = rootInfo.listFile(); let fileIterator = rootInfo.listFile();
// ListFile that contains the filter implementation. // listFile contains the filter implementation.
// let fileIterator = rootInfo.listFile(filter); // let fileIterator = rootInfo.listFile(filter);
if (!fileIterator) { if (!fileIterator) {
console.error("listFile interface returns an undefined object"); console.error("listFile interface returns an undefined object");
...@@ -233,7 +228,7 @@ Synchronously obtains the **FileIterator** object of the first-level files (file ...@@ -233,7 +228,7 @@ Synchronously obtains the **FileIterator** object of the first-level files (file
scanFile(filter?: Filter) : FileIterator scanFile(filter?: Filter) : FileIterator
Recursively obtains the **FileIterator** object of the files matching the conditions of the filter from the root node of a device synchronously. The **FileIterator** object then returns [FileInfo](#fileinfo) by using the [next()](#fileiteratornext). Recursively obtains the **FileIterator** object of the files matching the conditions of the filter from the device root node synchronously. The **FileIterator** object then returns [FileInfo](#fileinfo) by using [next()](#fileiteratornext).
**System capability**: SystemCapability.FileManagement.UserFileService **System capability**: SystemCapability.FileManagement.UserFileService
...@@ -241,39 +236,39 @@ Recursively obtains the **FileIterator** object of the files matching the condit ...@@ -241,39 +236,39 @@ Recursively obtains the **FileIterator** object of the files matching the condit
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| --- | --- | -- | -- | | --- | --- | -- | -- |
| filter | Filter | No| **Filter** object. | | filter | Filter | No| **Filter** object. |
**Return value** **Return value**
| Type| Description| | Type| Description|
| --- | -- | | --- | -- |
| FileIterator | **FileIterator** object obtained.| | FileIterator | **FileIterator** object obtained.|
**Example** **Example**
```js ```js
/ / Obtain root information by using getRoots(). // Obtain rootInfos by using getRoots().
// let filter = {suffix : [".txt", ".jpg", ".xlsx"]}; // let filter = {suffix : [".txt", ".jpg", ".xlsx"]};
let rootInfo = rootinfos[0]; let rootInfo = rootInfos[0];
let fileInfos = []; let fileInfos = [];
let isDone = false; let isDone = false;
try { try {
let fileIterator = rootInfo.scanFile(); let fileIterator = rootInfo.scanFile();
// ScanFile that contains the filter implementation // scanFile contains the filter implementation.
// let fileIterator = rootInfo.scanFile(filter); // let fileIterator = rootInfo.scanFile(filter);
if (!fileIterator) { if (!fileIterator) {
console.error("scanFile interface returns undefined object"); console.error("scanFile interface returns undefined object");
return; return;
} }
while (!isDone) { while (!isDone) {
let result = fileIterator.next(); let result = fileIterator.next();
console.log("next result = " + JSON.stringify(result)); console.log("next result = " + JSON.stringify(result));
isDone = result.done; isDone = result.done;
if (!isDone) if (!isDone)
fileInfos.push(result.value); fileInfos.push(result.value);
} }
} catch (error) { } catch (error) {
console.error("scanFile failed, error " + error); console.error("scanFile failed, error " + error);
} }
...@@ -291,15 +286,15 @@ Synchronously obtains the **FileIterator** object of the next-level files (file ...@@ -291,15 +286,15 @@ Synchronously obtains the **FileIterator** object of the next-level files (file
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| --- | --- | -- | -- | | --- | --- | -- | -- |
| filter | Filter | No| **Filter** object. | | filter | Filter | No| **Filter** object. |
**Return value** **Return value**
| Type| Description| | Type| Description|
| --- | -- | | --- | -- |
| FileIterator | **FileIterator** object obtained.| | FileIterator | **FileIterator** object obtained.|
**Example** **Example**
...@@ -311,7 +306,7 @@ Synchronously obtains the **FileIterator** object of the next-level files (file ...@@ -311,7 +306,7 @@ Synchronously obtains the **FileIterator** object of the next-level files (file
let isDone = false; let isDone = false;
try { try {
let fileIterator = fileInfoDir.listFile(); let fileIterator = fileInfoDir.listFile();
// ListFile that contains the filter implementation. // listFile contains the filter implementation.
// let fileIterator = rootInfo.listFile(filter); // let fileIterator = rootInfo.listFile(filter);
if (!fileIterator) { if (!fileIterator) {
console.error("listFile interface returns an undefined object"); console.error("listFile interface returns an undefined object");
...@@ -341,16 +336,16 @@ Recursively obtains the **FileIterator** object of the files matching the condit ...@@ -341,16 +336,16 @@ Recursively obtains the **FileIterator** object of the files matching the condit
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| --- | --- | -- | -- | | --- | --- | -- | -- |
| filter | Filter | No| **Filter** object. | | filter | Filter | No| **Filter** object. |
**Return value** **Return value**
| Type| Description| | Type| Description|
| --- | -- | | --- | -- |
| FileIterator | **FileIterator** object obtained.| | FileIterator | **FileIterator** object obtained.|
**Example** **Example**
...@@ -362,7 +357,7 @@ Recursively obtains the **FileIterator** object of the files matching the condit ...@@ -362,7 +357,7 @@ Recursively obtains the **FileIterator** object of the files matching the condit
let isDone = false; let isDone = false;
try { try {
let fileIterator = fileInfoDir.scanFile(); let fileIterator = fileInfoDir.scanFile();
// ScanFile that contains the filter implementation // scanFile contains the filter implementation.
// let fileIterator = rootInfo.scanFile(filter); // let fileIterator = rootInfo.scanFile(filter);
if (!fileIterator) { if (!fileIterator) {
console.error("scanFile interface returns an undefined object"); console.error("scanFile interface returns an undefined object");
...@@ -370,7 +365,7 @@ Recursively obtains the **FileIterator** object of the files matching the condit ...@@ -370,7 +365,7 @@ Recursively obtains the **FileIterator** object of the files matching the condit
} }
while (!isDone) { while (!isDone) {
let result = fileIterator.next(); let result = fileIterator.next();
console.error("next result = " + JSON.stringify(result)); console.log("next result = " + JSON.stringify(result));
isDone = result.done; isDone = result.done;
if (!isDone) if (!isDone)
subfileInfos.push(result.value); subfileInfos.push(result.value);
...@@ -392,25 +387,28 @@ Creates a file in a directory. This API uses a promise to return the result. ...@@ -392,25 +387,28 @@ Creates a file in a directory. This API uses a promise to return the result.
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| --- | --- | --- | -- | | --- | --- | --- | -- |
| uri | string | Yes| URI of the parent directory for the file to create.| | uri | string | Yes| URI of the parent directory for the file to create.|
| displayName | string | Yes| Name of the file to create. A file name extension must be added for a local file. It is not required for a file stored in a shared disk.| | displayName | string | Yes| Name of the file to create. A file name extension must be added for a local file. It is not required for a file stored in a shared disk.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| --- | -- | | --- | -- |
| Promise&lt;string&gt; | URI of the file created.| | Promise&lt;string&gt; | Promise used to return the URI of the file created.|
**Example** **Example**
```js ```js
// The URI of a shared disk is used as an example. // The media library URI is used as an example.
let sourceUri = "datashare:///com.ohos.UserFile.ExternalFileManager/data/storage/el1/bundle/storage_daemon"; // In the sample code, sourceUri indicates the Download directory. The URI is the URI in fileInfo.
// You can use the URI obtained.
let sourceUri = "datashare:///media/file/6";
let displayName = "file1" let displayName = "file1"
let fileUri = null; let fileUri = null;
try { try {
// Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper.
fileUri = await fileAccessHelper.createFile(sourceUri, displayName) fileUri = await fileAccessHelper.createFile(sourceUri, displayName)
if (!fileUri) { if (!fileUri) {
console.error("createFile return undefined object"); console.error("createFile return undefined object");
...@@ -434,25 +432,28 @@ Creates a folder in a directory. This API uses a promise to return the result. ...@@ -434,25 +432,28 @@ Creates a folder in a directory. This API uses a promise to return the result.
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| --- | --- | --- | -- | | --- | --- | --- | -- |
| parentUri | string | Yes| URI of the parent directory for the folder to create.| | parentUri | string | Yes| URI of the parent directory for the folder to create.|
| displayName | string | Yes| Name of the folder to create.| | displayName | string | Yes| Name of the folder to create.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| --- | -- | | --- | -- |
| Promise&lt;string&gt; | URI of the folder created.| | Promise&lt;string&gt; | Promise used to return the URI of the folder created.|
**Example** **Example**
```js ```js
// The URI of a shared disk is used as an example. // The media library URI is used as an example.
let sourceUri = "datashare:///com.ohos.UserFile.ExternalFileManager/data/storage/el1/bundle/storage_daemon"; // In the sample code, sourceUri indicates the Download directory. The URI is the URI in fileInfo.
// You can use the URI obtained.
let sourceUri = "datashare:///media/file/6";
let dirName = "dirTest" let dirName = "dirTest"
let dirUri = null; let dirUri = null;
try { try {
// Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper.
dirUri = await fileAccessHelper.mkDir(sourceUri, dirName) dirUri = await fileAccessHelper.mkDir(sourceUri, dirName)
if (!dirUri) { if (!dirUri) {
console.error("mkDir return undefined object"); console.error("mkDir return undefined object");
...@@ -466,7 +467,7 @@ Creates a folder in a directory. This API uses a promise to return the result. ...@@ -466,7 +467,7 @@ Creates a folder in a directory. This API uses a promise to return the result.
## FileAccessHelper.openFile ## FileAccessHelper.openFile
openFile(uri: string, flags: [OPENFLAGS](#openflags)) : Promise&lt;number&gt; openFile(uri: string, flags: OPENFLAGS) : Promise&lt;number&gt;
Opens a file. This API uses a promise to return the result. Opens a file. This API uses a promise to return the result.
...@@ -476,25 +477,28 @@ Opens a file. This API uses a promise to return the result. ...@@ -476,25 +477,28 @@ Opens a file. This API uses a promise to return the result.
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| --- | --- | --- | -- | | --- | --- | --- | -- |
| uri | string | Yes| URI of the file to open.| | uri | string | Yes| URI of the file to open.|
| flags | OPENFLAGS | |File open mode.| | flags | [OPENFLAGS](#openflags) | Yes| File open mode.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| --- | -- | | --- | -- |
| Promise&lt;number&gt | Handle of the file opened.| | Promise&lt;number&gt; | Promise used to return the handle to the file opened.|
**Example** **Example**
```js ```js
// The URI of a shared disk is used as an example. // The media library URI is used as an example.
let targetUri = "datashare:///com.ohos.UserFile.ExternalFileManager/data/storage/el1/bundle/storage_daemon/file1"; //In the sample code, targetUri indicates a file in the Download directory. The URI is the URI in fileInfo.
// You can use the URI obtained.
let targetUri = "datashare:///media/file/100";
try { try {
let fd = await fileAccessHelper.openFile(targetUri, OPENFLAGS.READ); // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper.
} cache (error) { let fd = await fileAccessHelper.openFile(targetUri, fileAccess.OPENFLAGS.READ);
} catch (error) {
console.error("openFile failed, error " + error); console.error("openFile failed, error " + error);
}; };
``` ```
...@@ -511,9 +515,9 @@ Deletes a file or folder. This API uses a promise to return the result. ...@@ -511,9 +515,9 @@ Deletes a file or folder. This API uses a promise to return the result.
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| --- | --- | --- | -- | | --- | --- | --- | -- |
| uri | string | Yes| URI of the file or folder to delete.| | uri | string | Yes| URI of the file or folder to delete.|
**Return value** **Return value**
...@@ -524,13 +528,16 @@ Deletes a file or folder. This API uses a promise to return the result. ...@@ -524,13 +528,16 @@ Deletes a file or folder. This API uses a promise to return the result.
**Example** **Example**
```js ```js
// The URI of a shared disk is used as an example. // The media library URI is used as an example.
let targetUri = "datashare:///com.ohos.UserFile.ExternalFileManager/data/storage/el1/bundle/storage_daemon/file1"; //In the sample code, targetUri indicates a file in the Download directory. The URI is the URI in fileInfo.
// You can use the URI obtained.
let targetUri = "datashare:///media/file/100";
try { try {
// Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper.
let code = await fileAccessHelper.delete(targetUri); let code = await fileAccessHelper.delete(targetUri);
if (code != 0) if (code != 0)
console.error("delete failed, code " + code); console.error("delete failed, code " + code);
} cache (error) { } catch (error) {
console.error("delete failed, error " + error); console.error("delete failed, error " + error);
}; };
``` ```
...@@ -547,27 +554,30 @@ Moves a file or folder. This API uses a promise to return the result. ...@@ -547,27 +554,30 @@ Moves a file or folder. This API uses a promise to return the result.
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| --- | --- | --- | -- | | --- | --- | --- | -- |
| sourceFile | string | Yes| URI of the file or folder to move.| | sourceFile | string | Yes| URI of the file or folder to move.|
| destFile | string | Yes| URI of the folder to which the file or folder is moved.| | destFile | string | Yes| URI of the folder to which the file or folder will be moved.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| ----- | ------ | | ----- | ------ |
| Promise&lt;string&gt; | URI of the file or folder in the destination directory.| | Promise&lt;string&gt; | Promise used to return the URI of the file or folder in the destination directory.|
**Example** **Example**
```js ```js
// The URI of a shared disk is used as an example. // The media library URI is used as an example.
let sourceFile = "datashare:///com.ohos.UserFile.ExternalFileManager/data/storage/el1/bundle/storage_daemon/file1"; //In the sample code, sourceFile destFile indicates the file or folder in the Download directory. The URI is the URI in fileInfo.
let destFile = "datashare:///com.ohos.UserFile.ExternalFileManager/data/storage/el1/bundle/storage_daemon/dirTest"; // You can use the URI obtained.
let sourceFile = "datashare:///media/file/102";
let destFile = "datashare:///media/file/101";
try { try {
// Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper.
let fileUri = await fileAccessHelper.move(sourceFile, destFile); let fileUri = await fileAccessHelper.move(sourceFile, destFile);
console.log("Operation successful. fileUri: " + JSON.stringify(fileUri)); console.log("Operation successful. fileUri: " + JSON.stringify(fileUri));
} cache (error) { } catch (error) {
console.error("move failed, error " + error); console.error("move failed, error " + error);
}; };
``` ```
...@@ -584,26 +594,29 @@ Renames a file or folder. This API uses a promise to return the result. ...@@ -584,26 +594,29 @@ Renames a file or folder. This API uses a promise to return the result.
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| --- | --- | --- | -- | | --- | --- | --- | -- |
| uri | string | Yes| URI of the file or folder to rename.| | uri | string | Yes| URI of the file or folder to rename.|
| displayName | string | Yes| New name of the file or folder, which can contain the file name extension.| | displayName | string | Yes| New name of the file or folder, which can contain the file name extension.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| --- | -- | | --- | -- |
| Promise&lt;string&gt; | URI of the renamed file or folder.| | Promise&lt;string&gt; | Promise used to return the URI of the renamed file or folder.|
**Example** **Example**
```js ```js
// The URI of a shared disk is used as an example. // The media library URI is used as an example.
let sourceDir = "datashare:///com.ohos.UserFile.ExternalFileManager/data/storage/el1/bundle/storage_daemon/dirTest"; // In the sample code, sourceDir indicates a file in the Download directory. The URI is the URI in fileInfo.
// You can use the URI obtained.
let sourceDir = "datashare:///media/file/100";
try { try {
// Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper.
let DestDir = await fileAccessHelper.rename(sourceDir, "testDir"); let DestDir = await fileAccessHelper.rename(sourceDir, "testDir");
console.log("Operation successful. DestDir: " + JSON.stringify(DestDir)); console.log("Operation successful. DestDir: " + JSON.stringify(DestDir));
} cache (error) { } catch (error) {
console.error("rename failed, error " + error); console.error("rename failed, error " + error);
}; };
``` ```
...@@ -620,9 +633,9 @@ Checks whether a file or folder exists. This API uses a promise to return the re ...@@ -620,9 +633,9 @@ Checks whether a file or folder exists. This API uses a promise to return the re
**Parameters** **Parameters**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| --- | --- | --- | -- | | --- | --- | --- | -- |
| sourceFileUri | string | Yes| URI of the file or folder.| | sourceFileUri | string | Yes| URI of the file or folder.|
**Return value** **Return value**
...@@ -633,15 +646,18 @@ Checks whether a file or folder exists. This API uses a promise to return the re ...@@ -633,15 +646,18 @@ Checks whether a file or folder exists. This API uses a promise to return the re
**Example** **Example**
```js ```js
// The URI of a shared disk is used as an example. // The media library URI is used as an example.
let sourceDir = "datashare:///com.ohos.UserFile.ExternalFileManager/data/storage/el1/bundle/storage_daemon/dirTest"; // In the sample code, sourceDir indicates a file in the Download directory. The URI is the URI in fileInfo.
// You can use the URI obtained.
let sourceDir = "datashare:///media/file/100";
try { try {
// Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper.
let existJudgment = await fileAccessHelper.access(sourceDir); let existJudgment = await fileAccessHelper.access(sourceDir);
if (existJudgment) if (existJudgment)
console.log("sourceDir exists"); console.log("sourceDir exists");
else else
console.log("sourceDir does not exist"); console.log("sourceDir does not exist");
} cache (error) { } catch (error) {
console.error("rename failed, error " + error); console.error("rename failed, error " + error);
}; };
``` ```
...@@ -656,10 +672,6 @@ Obtains the next-level device root directory. **RootIterator** is an iterator ob ...@@ -656,10 +672,6 @@ Obtains the next-level device root directory. **RootIterator** is an iterator ob
**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER **Required permissions**: ohos.permission.FILE_ACCESS_MANAGER
**Parameters**
None
**Return value** **Return value**
| Type| Description| | Type| Description|
...@@ -676,10 +688,6 @@ Obtains the information about the next-level file or folder. **FileIterator** is ...@@ -676,10 +688,6 @@ Obtains the information about the next-level file or folder. **FileIterator** is
**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER **Required permissions**: ohos.permission.FILE_ACCESS_MANAGER
**Parameters**
None
**Return value** **Return value**
| Type| Description| | Type| Description|
...@@ -720,7 +728,7 @@ Represents the file or folder attribute information and interface capabilities. ...@@ -720,7 +728,7 @@ Represents the file or folder attribute information and interface capabilities.
| mode | number | Yes| No| Permissions on the file or folder.| | mode | number | Yes| No| Permissions on the file or folder.|
| size | number | Yes| No| Size of the file or folder.| | size | number | Yes| No| Size of the file or folder.|
| mtime | number | Yes| No| Time when the file or folder was last modified.| | mtime | number | Yes| No| Time when the file or folder was last modified.|
| mimeType | string | Yes| No| Media resource type of the file or folder.| | mimeType | string | Yes| No| MIME type of the file or folder.|
## OPENFLAGS ## OPENFLAGS
......
# User File Extension Info # @ohos.fileExtensionInfo
The **fileExtensionInfo** module defines attributes in **RootInfo** and **FileInfo** of the user file access and management module. The **fileExtensionInfo** module defines attributes in **RootInfo** and **FileInfo** of the user file access and management module.
...@@ -37,10 +37,10 @@ Defines the values of **deviceFlags** used in **RootInfo**. **deviceFlags** is u ...@@ -37,10 +37,10 @@ Defines the values of **deviceFlags** used in **RootInfo**. **deviceFlags** is u
### Attributes ### Attributes
| Name| Type | Value| Readable| Writable| Description | | Name| Type | Readable| Writable| Description |
| ------ | ------ | ---- | ---- | ---- | -------- | | ------ | ------ | ---- | ---- | -------- |
| SUPPORTS_READ | number | 0b1 | Yes | No | Read support.| | SUPPORTS_READ | number | Yes | No | The device supports read.|
| SUPPORTS_WRITE | number | 0b10 | Yes | No | Write support.| | SUPPORTS_WRITE | number | Yes | No | This device supports write.|
## fileExtensionInfo.DocumentFlag ## fileExtensionInfo.DocumentFlag
...@@ -50,9 +50,9 @@ Defines the values of **mode** used in **FileInfo**. ...@@ -50,9 +50,9 @@ Defines the values of **mode** used in **FileInfo**.
### Attributes ### Attributes
| Name| Type | Value| Readable| Writable| Description | | Name| Type | Readable| Writable| Description |
| ------ | ------ | ---- | ---- | ---- | -------- | | ------ | ------ | ---- | ---- | -------- |
| REPRESENTS_FILE | number | 0b1 | Yes | No | File.| | REPRESENTS_FILE | number | Yes | No | File.|
| REPRESENTS_DIR | number | 0b10 | Yes | No | Directory.| | REPRESENTS_DIR | number | Yes | No | Directory.|
| SUPPORTS_READ | number | 0b100 | Yes | No | Read support.| | SUPPORTS_READ | number | Yes | No | This file is readable.|
| SUPPORTS_WRITE | number | 0b1000 | Yes | No | Write support.| | SUPPORTS_WRITE | number | Yes | No | This file is writable.|
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册