# @ohos.file.fileAccess (User File Access and Management)
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.
The **fileAccess** module provides a framework for accessing and operating user files based on the ExtensionAbility mechanism. This module interacts with file management services, such as the media library and external storage management service, and provides a set of unified interfaces for system applications to access and manage files. The media library service allows access to user files on local 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**
>
>- 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 **Files**.
>- The APIs of this module support processing of error codes. For details, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
> - 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 **FileManager**.
## Modules to Import
...
...
@@ -16,9 +15,11 @@ import fileAccess from '@ohos.file.fileAccess';
Obtains information about all wants with **extension** set to **fileAccess** in the system. A want is a basic communication component used to start services. This API uses a promise to return the result.
Obtains information about all Wants with **extension** set to **fileAccess** in the system. A Want contains information for starting an ability. This API uses a promise to return the result.
**Model restriction**: This API can be used only in the stage model.
Obtains information about all Wants with **extension** set to **fileAccess** in the system. A Want contains information for starting an ability. This API uses an asynchronous callback to return the result.
Obtains information about all wants with **extension** set to **fileAccess** in the system. A want is a basic communication component used to start services. This API uses an asynchronous callback to return the result.
**Model restriction**: This API can be used only in the stage model.
| uri | string | Yes| No| URI of the file or directory.|
| fileName | string | Yes| No| Name of the file or directory.|
| mode | number | Yes| No| Permissions on the file or directory.|
| size | number | Yes| No| Size of the file or directory.|
| mtime | number | Yes| No| Time when the file or directory was last modified.|
| mimeType | string | Yes| No| Multipurpose Internet Mail Extensions (MIME) type of the file or directory.|
### listFile
getRoots( ) : Promise<RootIterator>
listFile(filter?: Filter) : FileIterator
Synchronously obtains a **FileIterator** object that lists the next-level files (directories) matching the conditions of the filter from a directory and returns [FileInfo](#fileinfo) using [next()](#next). Currently, only built-in storage devices support the file filter.
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).
**Model restriction**: This API can be used only in the stage model.
Obtains information about the device root nodes of the file management service type connected to the **Helper** object. This API uses an asynchronous callback to return the result.
Synchronously obtains a **FileIterator** object that recursively retrieves the files matching the conditions of the filter from a directory and returns [FileInfo](#fileinfo) using [next()](#next). Currently, this API supports only built-in storage devices.
The callback has a **RootIterator** object, which returns [RootInfo](#rootinfo) through [next()](#rootiteratornext).
**Model restriction**: This API can be used only in the stage model.
Synchronously obtains the **FileIterator** object of the first-level files (directory) matching the conditions of the filter from the device root node. The **FileIterator** object then returns [FileInfo](#fileinfo) by using [next()](#fileiteratornext).
**Model restriction**: This API can be used only in the stage model.
| {value: [FileInfo](#fileinfo), done: boolean} | File or directory information obtained. This method traverses the specified directory until **true** is returned. The **value** field contains the file or directory information obtained.|
**Error codes**
For details about error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
## RootInfo
Provides the device's root attribute information and APIs.
**Model restriction**: This API can be used only in the stage model.
| deviceType | number | Yes| No|Type of the device.|
| uri | string | Yes| No| Root directory URI of the device.|
| displayName | string | Yes| No| Device name.|
| deviceFlags | number | Yes| No| Capabilities supported by the device.|
### listFile
listFile(filter?: Filter) : FileIterator
Synchronously obtains a **FileIterator** object that lists the first-level files (directories) matching the conditions of the filter from the device root directory and returns [FileInfo](#fileinfo) using [next()](#next). Currently, only built-in storage devices support the file filter.
**Model restriction**: This API can be used only in the stage model.
For details about error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
**Example**
...
...
@@ -285,7 +409,7 @@ Synchronously obtains the **FileIterator** object of the first-level files (dire
letisDone=false;
try{
letfileIterator=rootInfo.listFile();
// listFile contains the filter implementation.
// listFile() with the filter implementation.
// let fileIterator = rootInfo.listFile(filter);
if(!fileIterator){
console.error("listFile interface returns an undefined object");
...
...
@@ -303,11 +427,13 @@ Synchronously obtains the **FileIterator** object of the first-level files (dire
}
```
## RootInfo.scanFile
### scanFile
scanFile(filter?: Filter) : FileIterator
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).
Synchronously obtains a **FileIterator** object that recursively retrieves the files matching the conditions of the filter from the device root directory and returns [FileInfo](#fileinfo)using [next()](#next). Currently, this API supports only built-in storage devices.
**Model restriction**: This API can be used only in the stage model.
@@ -353,61 +483,106 @@ Recursively obtains the **FileIterator** object of the files matching the condit
}
```
## FileInfo.listfile
## RootIterator
listFile(filter?: Filter) : FileIterator
Provides an iterator object of the device root directory.
Synchronously obtains the **FileIterator** object of the next-level files (directories) matching the conditions of the filter from a directory. The **FileIterator** object then returns [FileInfo](#fileinfo) by using [next()](#fileiteratornext).
**Model restriction**: This API can be used only in the stage model.
| {value: [RootInfo](#rootinfo), done: boolean} | Root directory information obtained. This method traverses the directory until **true** is returned. The **value** field contains the root directory information.|
**Error codes**
For details about error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
## FileAccessHelper
Provides a **FileAccessHelper** object.
**Model restriction**: This API can be used only in the stage model.
Obtains information about the device root nodes of the file management service connected to the **Helper** object. This API uses a promise to return a **RootIterator** object,
which returns [RootInfo](#rootinfo) by using [next](#next-1).
**Model restriction**: This API can be used only in the stage model.
Recursively obtains the **FileIterator** object of the files matching the conditions of the filter from a directory synchronously. The **FileIterator** object then returns [FileInfo](#fileinfo) by using [next()](#fileiteratornext).
Obtains information about the device root nodes of the file management service connected to the **Helper** object. This API uses an asynchronous callback to return a **RootIterator** object,
which returns [RootInfo](#rootinfo) by using [next](#next-1).
**Model restriction**: This API can be used only in the stage model.
Moves a file or directory. This API uses a promise to return the result.
Moves a file or directory. This API uses a promise to return the result. Currently, this API does not support move of files or directories across devices.
**Model restriction**: This API can be used only in the stage model.
Moves a file or directory. This API uses an asynchronous callback to return the result.
Moves a file or directory. This API uses an asynchronous callback to return the result. Currently, this API does not support move of files or directories across devices.
**Model restriction**: This API can be used only in the stage model.
| {value: RootInfo, done: boolean} | Root directory information obtained. This API traverses the directory until **done** returns **true**. The **value** field contains the root directory information.|
## FileIterator.next
next( ) : { value: FileInfo, done: boolean }
Obtains the information about the next-level file or directory. **FileIterator** is an iterator object of a directory.
| {value: FileInfo, done: boolean} | File or directory information obtained. This API traverses the specified directory until **done** returns **true**. The **value** field contains the file or directory information obtained.|
## RootInfo
Represents the root attribute information and interface capabilities of a device.