The fileio module provides APIs for file storage and management, including basic file management, directory management, file information statistics, and stream read and write.
The **fileio** module provides APIs for file storage and management, including basic file management, directory management, file information statistics, and stream read and write.
> **NOTE**<br>
> **NOTE**<br>
> The initial APIs of this module are supported since API version 6. 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 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.
...
@@ -24,7 +24,7 @@ import Ability from '@ohos.application.Ability';
...
@@ -24,7 +24,7 @@ import Ability from '@ohos.application.Ability';
| path | string | Yes | Application sandbox path of the file. |
| path | string | Yes | Application sandbox path of the file. |
| flags | number | Yes | Option for opening the file. You must specify one of the following options. By default, the file is open in read-only mode.<br>- **0o0**: Open the file in read-only mode.<br>- **0o1**: Open the file in write-only mode.<br>- **0o2**: Open the file in read/write mode.<br>In addition, you can specify the following options, separated using a bitwise OR operator (|). By default, no additional option is specified.<br>- **0o100**: If the file does not exist, create it. If you use this option, you must also specify **mode**.<br>- **0o200**: If **0o100** is added and the file already exists, throw an exception.<br>- **0o1000**: If the file exists and is open in write-only or read/write mode, truncate the file length to 0.<br>- **0o2000**: Open the file in append mode. New data will be appended to the file (added to the end of the file).<br>- **0o4000**: If **path** points to a named pipe (also known as a FIFO), block special file, or character special file, perform non-blocking operations on the open file and in subsequent I/Os.<br>- **0o200000**: If **path** does not point to a directory, throw an exception.<br><br>- **0o400000**: If **path** points to a symbolic link, throw an exception.<br>- **0o4010000**: Open the file in synchronous I/O mode.|
| flags | number | No | Option for opening the file. You must specify one of the following options. By default, the file is open in read-only mode.<br>- **0o0**: Open the file in read-only mode.<br>- **0o1**: Open the file in write-only mode.<br>- **0o2**: Open the file in read/write mode.<br>In addition, you can specify the following options, separated using a bitwise OR operator (|). By default, no additional option is specified.<br>- **0o100**: If the file does not exist, create it. If you use this option, you must also specify **mode**.<br>- **0o200**: If **0o100** is added and the file already exists, throw an exception.<br>- **0o1000**: If the file exists and is open in write-only or read/write mode, truncate the file length to 0.<br>- **0o2000**: Open the file in append mode. New data will be appended to the file (added to the end of the file).<br>- **0o4000**: If **path** points to a named pipe (also known as a FIFO), block special file, or character special file, perform non-blocking operations on the open file and in subsequent I/Os.<br>- **0o200000**: If **path** does not point to a directory, throw an exception.<br><br>- **0o400000**: If **path** points to a symbolic link, throw an exception.<br>- **0o4010000**: Open the file in synchronous I/O mode.|
| mode | number | Yes | Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (|). The default value is **0o666**.<br>- **0o666**: The owner, user group, and other users have the read and write permissions on the file.<br>- **0o700**: The owner has the read, write, and execute permissions.<br>- **0o400**: The owner has the read permission.<br>- **0o200**: The owner has the write permission.<br>- **0o100**: The owner has the execute permission.<br>- **0o070**: The user group has the read, write, and execute permissions.<br>- **0o040**: The user group has the read permission.<br>- **0o020**: The user group has the write permission.<br>- **0o010**: The user group has the execute permission.<br>- **0o007**: Other users have the read, write, and execute permissions.<br>- **0o004**: Other users have the read permission.<br>- **0o002**: Other users have the write permission.<br>- **0o001**: Other users have the execute permission.|
| mode | number | No | Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (|). The default value is **0o666**.<br>- **0o666**: The owner, user group, and other users have the read and write permissions on the file.<br>- **0o700**: The owner has the read, write, and execute permissions.<br>- **0o400**: The owner has the read permission.<br>- **0o200**: The owner has the write permission.<br>- **0o100**: The owner has the execute permission.<br>- **0o070**: The user group has the read, write, and execute permissions.<br>- **0o040**: The user group has the read permission.<br>- **0o020**: The user group has the write permission.<br>- **0o010**: The user group has the execute permission.<br>- **0o007**: Other users have the read, write, and execute permissions.<br>- **0o004**: Other users have the read permission.<br>- **0o002**: Other users have the write permission.<br>- **0o001**: Other users have the execute permission.|
| callback | AsyncCallback <void> | Yes | Callback invoked when the file is open asynchronously. |
| callback | AsyncCallback <void> | Yes | Callback invoked when the file is open asynchronously. |
**Example**
**Example**
```js
```js
fileio.open(path,0,function(err,fd){
letfilePath=pathDir+"/test.txt";
fileio.open(filePath,0,function(err,fd){
// Do something.
// Do something.
});
});
```
```
...
@@ -603,7 +616,7 @@ Opens a file. This API uses an asynchronous callback to return the result.
...
@@ -603,7 +616,7 @@ Opens a file. This API uses an asynchronous callback to return the result.
## fileio.openSync
## fileio.openSync
openSync(path:string, flags?:number, mode?:number): number
openSync(path: string, flags?: number, mode?: number): number
| filePath | string | Yes | Application sandbox path of the file to read. |
| filePath | string | Yes | Application sandbox path of the file to read. |
| options | Object | Yes | The options are as follows:<br>- **position** (number): position of the data to read in the file. By default, data is read from the current position.<br>- **length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>- **encoding**: format of the string to be encoded. The default value is **utf-8**, which is the only value supported.|
| options | Object | No | The options are as follows:<br>- **position** (number): position of the data to read in the file. By default, data is read from the current position.<br>- **length** (number): length of the data to read. The default value is the buffer length minus the offset.<br>- **encoding**: format of the string to be encoded. The default value is **utf-8**, which is the only value supported.|
| callback | AsyncCallback<string> | Yes | Callback used to return the content read. |
| callback | AsyncCallback<string> | Yes | Callback used to return the content read. |
| filename | string | Yes | Application sandbox path of the file. |
| filePath | string | Yes | Application sandbox path of the file. |
| events | Number | Yes | - **1**: The file or directory is renamed.<br>- **2**: The file or directory is modified.<br>- **3**: The file or directory is modified and renamed.|
| events | Number | Yes | - **1**: The file or directory is renamed.<br>- **2**: The file or directory is modified.<br>- **3**: The file or directory is modified and renamed.|
| callback | AsyncCallback<number > | Yes | Called each time a change is detected. |
| callback | AsyncCallback<number > | Yes | Called each time a change is detected. |
...
@@ -2527,8 +2598,8 @@ Listens for file or directory changes. This API uses an asynchronous callback to
...
@@ -2527,8 +2598,8 @@ Listens for file or directory changes. This API uses an asynchronous callback to
**Example**
**Example**
```js
```js
letfilename=path+"/test.txt";
letfilePath=pathDir+"/test.txt";
fileio.createWatcher(filename,1,function(number){
fileio.createWatcher(filePath,1,function(number){
console.info("Monitoring times: "+number);
console.info("Monitoring times: "+number);
});
});
...
@@ -2589,7 +2660,8 @@ Checks whether this file is a block special file. A block special file supports
...
@@ -2589,7 +2660,8 @@ Checks whether this file is a block special file. A block special file supports
@@ -2771,6 +2849,7 @@ Stops the **watcher** instance. This API uses an asynchronous callback to return
...
@@ -2771,6 +2849,7 @@ Stops the **watcher** instance. This API uses an asynchronous callback to return
})
})
```
```
## Stream
## Stream
Provides file stream management. Before calling a method of the **Stream** class, use the **createStream()** method synchronously or asynchronously to create a **Stream** instance.
Provides file stream management. Before calling a method of the **Stream** class, use the **createStream()** method synchronously or asynchronously to create a **Stream** instance.
...
@@ -2793,7 +2872,8 @@ Closes the stream. This API uses a promise to return the result.
...
@@ -2793,7 +2872,8 @@ Closes the stream. This API uses a promise to return the result.
**Example**
**Example**
```js
```js
letss=fileio.createStreamSync(path,"r+");
letfilePath=pathDir+"/test.txt";
letss=fileio.createStreamSync(filePath,"r+");
ss.close().then(function(){
ss.close().then(function(){
console.info("File stream closed");
console.info("File stream closed");
}).catch(function(err){
}).catch(function(err){
...
@@ -2819,9 +2899,10 @@ Closes the stream. This API uses an asynchronous callback to return the result.
...
@@ -2819,9 +2899,10 @@ Closes the stream. This API uses an asynchronous callback to return the result.
**Example**
**Example**
```js
```js
letss=fileio.createStreamSync(path,"r+");
letfilePath=pathDir+"/test.txt";
letss=fileio.createStreamSync(filePath,"r+");
ss.close(function(err){
ss.close(function(err){
// Do something
// Do something.
});
});
```
```
...
@@ -2837,7 +2918,8 @@ Synchronously closes the stream.
...
@@ -2837,7 +2918,8 @@ Synchronously closes the stream.
**Example**
**Example**
```js
```js
letss=fileio.createStreamSync(path,"r+");
letfilePath=pathDir+"/test.txt";
letss=fileio.createStreamSync(filePath,"r+");
ss.closeSync();
ss.closeSync();
```
```
...
@@ -2859,7 +2941,8 @@ Flushes the stream. This API uses a promise to return the result.
...
@@ -2859,7 +2941,8 @@ Flushes the stream. This API uses a promise to return the result.
**Example**
**Example**
```js
```js
letss=fileio.createStreamSync(path,"r+");
letfilePath=pathDir+"/test.txt";
letss=fileio.createStreamSync(filePath,"r+");
ss.flush().then(function(){
ss.flush().then(function(){
console.info("Stream flushed");
console.info("Stream flushed");
}).catch(function(err){
}).catch(function(err){
...
@@ -2885,9 +2968,10 @@ Flushes the stream. This API uses an asynchronous callback to return the result.
...
@@ -2885,9 +2968,10 @@ Flushes the stream. This API uses an asynchronous callback to return the result.
**Example**
**Example**
```js
```js
letss=fileio.createStreamSync(path,"r+");
letfilePath=pathDir+"/test.txt";
letss=fileio.createStreamSync(filePath,"r+");
ss.flush(function(err){
ss.flush(function(err){
// Do something
// Do something.
});
});
```
```
...
@@ -2903,7 +2987,8 @@ Synchronously flushes the stream.
...
@@ -2903,7 +2987,8 @@ Synchronously flushes the stream.
**Example**
**Example**
```js
```js
letss=fileio.createStreamSync(path,"r+");
letfilePath=pathDir+"/test.txt";
letss=fileio.createStreamSync(filePath,"r+");
ss.flushSync();
ss.flushSync();
```
```
...
@@ -2937,7 +3022,8 @@ Writes data into the stream. This API uses a promise to return the result.
...
@@ -2937,7 +3022,8 @@ Writes data into the stream. This API uses a promise to return the result.