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.
>**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 the file manager app.
Obtains information about all wants with **extension** set to **fileAcesss** 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 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
returns [RootInfo](#rootinfo) by using [next()](#rootiteratornext).
console.error("getRoots interface returns an undefined object");
return;
}
while(!isDone){
letresult=rootIterator.next();
console.log("next result = "+JSON.stringify(result));
isDone=result.done;
if(!isDone)
rootinfos.push(result.value);
}
}catch(error){
console.error("getRoots failed, error "+error);
}
}
```
## RootInfo.listfile
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).
// let filter = {suffix : [".txt", ".jpg", ".xlsx"]};
letrootInfo=rootinfos[0];
letfileInfos=[];
letisDone=false;
try{
letfileIterator=rootInfo.listFile();
// ListFile that contains the filter implementation.
// let fileIterator = rootInfo.listFile(filter);
if(fileIterator){
console.error("listFile interface returns an undefined object");
return;
}
while(!isDone){
letresult=fileIterator.next();
console.log("next result = "+JSON.stringify(result));
isDone=result.done;
if(!isDone)
fileInfos.push(result.value);
}
}catch(error){
console.log("listFile failed, error "+error);
}
```
## RootInfo.scanFile
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).
console.log("next result = "+JSON.stringify(result));
isDone=result.done;
if(!isDone)
fileInfos.push(result.value);
}
}catch(error){
console.error("scanFile failed, error "+error);
}
```
## FileInfo.listfile
listFile(filter?: Filter) : FileIterator
Synchronously obtains the **FileIterator** object of the next-level files (file folders) matching the conditions of the filter from a directory. The **FileIterator** object then returns [FileInfo](#fileinfo) by using [next()](#fileiteratornext).
// let filter = {suffix : [".txt", ".jpg", ".xlsx"]};
letfileInfoDir=fileInfos[0];
letsubfileInfos=[];
letisDone=false;
try{
letfileIterator=fileInfoDir.listFile();
// ListFile that contains the filter implementation.
// let fileIterator = rootInfo.listFile(filter);
if(fileIterator){
console.error("listFile interface returns an undefined object");
return;
}
while(!isDone){
letresult=fileIterator.next();
console.log("next result = "+JSON.stringify(result));
isDone=result.done;
if(!isDone)
subfileInfos.push(result.value);
}
}catch(error){
console.error("listFile failed, error "+error);
}
```
## FileInfo.scanfile
scanFile(filter?: Filter) : FileIterator;
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).
| 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.|
**Return value**
| Type| Description|
| --- | -- |
| Promise<string> | URI of the file created.|
**Example**
```js
// The URI of a shared disk is used as an example.
| {value: RootInfo, done: boolean} | Root directory information obtained. This API traverses the folder 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 folder. **FileIterator** is an iterator object of a folder.
| {value: FileInfo, done: boolean} | File or folder information obtained. This API traverses the specified folder until **done** returns **true**. The **value** field contains the file or folder information obtained.|
## RootInfo
Represents the root attribute information and interface capabilities of a device.
The **fileExtensionInfo** module defines attributes in **RootInfo** and **FileInfo** of the user file access and management module.
>**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.
Defines the values of **deviceFlags** used in **RootInfo**. **deviceFlags** is used to determine whether a capability is available through the AND operation.