提交 42049387 编写于 作者: A Annie_wang
上级 1a49c90c
......@@ -2,9 +2,10 @@
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**
>
> 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 APIs provided by this module are deprecated since API version 9. You are advised to use [@ohos.file.fs](./js-apis-file-fs.md).
## Modules to Import
......@@ -21,6 +22,7 @@ Stage Model
```js
import Ability from '@ohos.application.Ability';
class MainAbility extends Ability {
onWindowStageCreate(windowStage) {
let context = this.context;
......@@ -29,19 +31,20 @@ class MainAbility extends Ability {
}
```
For details about how to obtain the stage model context, see [Stage Model](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis/js-apis-ability-context.md#abilitycontext).
For details about how to obtain the stage model context, see [AbilityContext](js-apis-ability-context.md#abilitycontext).
FA Model
```js
import featureAbility from '@ohos.ability.featureAbility';
let context = featureAbility.getContext();
context.getFilesDir().then((data) => {
let pathDir = data;
})
```
For details about how to obtain the context of the FA model, see [FA Model](https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis/js-apis-Context.md#context).
For details about how to obtain the FA model context, see [Contex](js-apis-inner-app-context.md#context).
## fileio.stat
......@@ -67,10 +70,10 @@ Obtains file information. This API uses a promise to return the result.
```js
let filePath = pathDir + "test.txt";
fileio.stat(filePath).then(function(stat){
console.info("Got file info:"+ JSON.stringify(stat));
}).catch(function(err){
console.info("Failed to get file info. Error:"+ err);
fileio.stat(filePath).then(function (stat) {
console.info("getFileInfo succeed, the size of file is " + stat.size);
}).catch(function (err) {
console.info("getFileInfo failed with error:" + err);
});
```
......@@ -152,10 +155,10 @@ Opens a file directory. This API uses a promise to return the result.
```js
let dirPath = pathDir + "/testDir";
fileio.opendir(dirPath).then(function(dir){
console.info("Directory opened:"+ JSON.stringify(dir));
}).catch(function(err){
console.info("Failed to open the directory. Error:"+ err);
fileio.opendir(dirPath).then(function (dir) {
console.info("opendir succeed");
}).catch(function (err) {
console.info("opendir failed with error:" + err);
});
```
......@@ -240,17 +243,17 @@ Checks whether the current process can access a file. This API uses a promise to
```js
let filePath = pathDir + "/test.txt";
fileio.access(filePath).then(function() {
fileio.access(filePath).then(function () {
console.info("Access successful");
}).catch(function(err){
console.info("Access failed. Error:"+ err);
}).catch(function (err) {
console.info("access failed with error:" + err);
});
```
## fileio.access
access(path: string, mode: number, callback: AsyncCallback&lt;void&gt;): void
access(path: string, mode?: number, callback: AsyncCallback&lt;void&gt;): void
Checks whether the current process can access a file. This API uses an asynchronous callback to return the result.
......@@ -296,7 +299,7 @@ Synchronously checks whether the current process can access the specified file.
try {
fileio.accessSync(filePath);
} catch(err) {
console.info("accessSync failed with error:"+ err);
console.info("accessSync failed with error:" + err);
}
```
......@@ -326,10 +329,10 @@ Closes a file. This API uses a promise to return the result.
```js
let filePath = pathDir + "/test.txt";
let fd = fileio.openSync(filePath);
fileio.close(fd).then(function(){
fileio.close(fd).then(function () {
console.info("File closed");
}).catch(function(err){
console.info("Failed to close the file. Error:"+ err);
}).catch(function (err) {
console.info("close file failed with error:" + err);
});
```
......@@ -385,7 +388,7 @@ Synchronously closes a file.
## fileio.copyFile
copyFile(src: string | number, dest: string | number, mode?: number): Promise&lt;void&gt;
copyFile(src: string|number, dest: string|number, mode?: number): Promise&lt;void&gt;
Copies a file. This API uses a promise to return the result.
......@@ -395,8 +398,8 @@ Copies a file. This API uses a promise to return the result.
| Name | Type | Mandatory | Description |
| ---- | -------------------------- | ---- | ---------------------------------------- |
| src | string&nbsp;\|&nbsp;number | Yes | Path or file descriptor of the file to copy. |
| dest | string&nbsp;\|&nbsp;number | Yes | Path or file descriptor of the new file. |
| src | string\|number | Yes | Path or file descriptor of the file to copy. |
| dest | string\|number | Yes | Path or file descriptor of the new file. |
| mode | number | No | Option for overwriting the file of the same name in the destination path. The default value is **0**, which is the only value supported.<br>**0**: Completely overwrite the file with the same name and truncate the part that is not overwritten.|
**Return value**
......@@ -410,17 +413,17 @@ Copies a file. This API uses a promise to return the result.
```js
let srcPath = pathDir + "srcDir/test.txt";
let dstPath = pathDir + "dstDir/test.txt";
fileio.copyFile(srcPath, dstPath).then(function(){
fileio.copyFile(srcPath, dstPath).then(function () {
console.info("File copied");
}).catch(function(err){
console.info("Failed to copy the file. Error:"+ err);
}).catch(function (err) {
console.info("copyFile failed with error:" + err);
});
```
## fileio.copyFile
copyFile(src: string | number, dest: string | number, mode: number, callback: AsyncCallback&lt;void&gt;): void
copyFile(src: string|number, dest: string|number, mode: number, callback: AsyncCallback&lt;void&gt;): void
Copies a file. This API uses an asynchronous callback to return the result.
......@@ -430,8 +433,8 @@ Copies a file. This API uses an asynchronous callback to return the result.
| Name | Type | Mandatory | Description |
| -------- | -------------------------- | ---- | ---------------------------------------- |
| src | string&nbsp;\|&nbsp;number | Yes | Path or file descriptor of the file to copy. |
| dest | string&nbsp;\|&nbsp;number | Yes | Path or file descriptor of the new file. |
| src | string\|number | Yes | Path or file descriptor of the file to copy. |
| dest | string\|number | Yes | Path or file descriptor of the new file. |
| mode | number | No | Option for overwriting the file of the same name in the destination path. The default value is **0**, which is the only value supported.<br>**0**: Completely overwrite the file with the same name and truncate the part that is not overwritten.|
| callback | AsyncCallback&lt;void&gt; | Yes | Callback invoked when the file is copied asynchronously. |
......@@ -448,7 +451,7 @@ Copies a file. This API uses an asynchronous callback to return the result.
## fileio.copyFileSync
copyFileSync(src: string | number, dest: string | number, mode?: number): void
copyFileSync(src: string|number, dest: string|number, mode?: number): void
Synchronously copies a file.
......@@ -458,8 +461,8 @@ Synchronously copies a file.
| Name | Type | Mandatory | Description |
| ---- | -------------------------- | ---- | ---------------------------------------- |
| src | string&nbsp;\|&nbsp;number | Yes | Path or file descriptor of the file to copy. |
| dest | string&nbsp;\|&nbsp;number | Yes | Path or file descriptor of the new file. |
| src | string\|number | Yes | Path or file descriptor of the file to copy. |
| dest | string\|number | Yes | Path or file descriptor of the new file. |
| mode | number | No | Option for overwriting the file of the same name in the destination path. The default value is **0**, which is the only value supported.<br>**0**: Completely overwrite the file with the same name and truncate the part that is not overwritten.|
**Example**
......@@ -496,10 +499,10 @@ Creates a directory. This API uses a promise to return the result.
```js
let dirPath = pathDir + '/testDir';
fileio.mkdir(dirPath).then(function() {
fileio.mkdir(dirPath).then(function () {
console.info("Directory created");
}).catch(function (error){
console.info("Failed to create the directory. Error:"+ error);
}).catch(function (error) {
console.info("mkdir failed with error:" + error);
});
```
......@@ -524,7 +527,7 @@ Creates a directory. This API uses an asynchronous callback to return the result
```js
let dirPath = pathDir + '/testDir';
fileio.mkdir(dirPath, function(err) {
fileio.mkdir(dirPath, function (err) {
console.info("Directory created");
});
```
......@@ -579,10 +582,10 @@ Opens a file. This API uses a promise to return the result.
```js
let filePath = pathDir + "/test.txt";
fileio.open(filePath, 0o1, 0o0200).then(function(number){
fileio.open(filePath, 0o1, 0o0200).then(function (number) {
console.info("File opened");
}).catch(function(err){
console.info("Failed to open the file. Error:"+ err);
}).catch(function (err) {
console.info("open file failed with error:" + err);
});
```
......@@ -602,13 +605,13 @@ Opens a file. This API uses an asynchronous callback to return the result.
| path | string | Yes | Application sandbox path of the file. |
| 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>-&nbsp;**0o0**: Open the file in read-only mode.<br>-&nbsp;**0o1**: Open the file in write-only mode.<br>-&nbsp;**0o2**: Open the file in read/write mode.<br>In addition, you can specify the following options, separated using a bitwise OR operator (&#124;). By default, no additional option is specified.<br>-&nbsp;**0o100**: If the file does not exist, create it. If you use this option, you must also specify **mode**.<br>-&nbsp;**0o200**: If **0o100** is added and the file already exists, throw an exception.<br>-&nbsp;**0o1000**: If the file exists and is open in write-only or read/write mode, truncate the file length to 0.<br>-&nbsp;**0o2000**: Open the file in append mode. New data will be appended to the file (added to the end of the file).<br>-&nbsp;**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>-&nbsp;**0o200000**: If **path** does not point to a directory, throw an exception.<br><br>-&nbsp;**0o400000**: If **path** points to a symbolic link, throw an exception.<br>-&nbsp;**0o4010000**: Open the file in synchronous I/O mode.|
| mode | number | No | Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (&#124;). The default value is **0o666**.<br>-&nbsp;**0o666**: The owner, user group, and other users have the read and write permissions on the file.<br>-&nbsp;**0o700**: The owner has the read, write, and execute permissions.<br>- &nbsp;**0o400**: The owner has the read permission.<br>-&nbsp;**0o200**: The owner has the write permission.<br>-&nbsp;**0o100**: The owner has the execute permission.<br>-&nbsp;**0o070**: The user group has the read, write, and execute permissions.<br>-&nbsp;**0o040**: The user group has the read permission.<br>-&nbsp;**0o020**: The user group has the write permission.<br>-&nbsp;**0o010**: The user group has the execute permission.<br>-&nbsp;**0o007**: Other users have the read, write, and execute permissions.<br>-&nbsp;**0o004**: Other users have the read permission.<br>-&nbsp;**0o002**: Other users have the write permission.<br>-&nbsp;**0o001**: Other users have the execute permission.|
| callback | AsyncCallback&nbsp;&lt;void&gt; | Yes | Callback invoked when the file is open asynchronously. |
| callback | AsyncCallback&lt;number&gt; | Yes | Callback invoked when the file is open asynchronously. |
**Example**
```js
let filePath = pathDir + "/test.txt";
fileio.open(filePath, 0, function(err, fd) {
fileio.open(filePath, 0, function (err, fd) {
// Do something.
});
```
......@@ -655,11 +658,7 @@ Synchronously opens a file.
## fileio.read
read(fd: number, buffer: ArrayBuffer, options?: {
offset?: number;
length?: number;
position?: number;
}): Promise&lt;ReadOut&gt;
read(fd: number, buffer: ArrayBuffer, options?: { offset?: number; length?: number; position?: number; }): Promise&lt;ReadOut&gt;
Reads data from a file. This API uses a promise to return the result.
......@@ -685,22 +684,18 @@ Reads data from a file. This API uses a promise to return the result.
let filePath = pathDir + "/test.txt";
let fd = fileio.openSync(filePath, 0o2);
let buf = new ArrayBuffer(4096);
fileio.read(fd, buf).then(function(readOut){
fileio.read(fd, buf).then(function (readOut) {
console.info("Read file data successfully");
console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
}).catch(function(err){
console.info("Failed to read file data. Error:"+ err);
}).catch(function (err) {
console.info("read file data failed with error:" + err);
});
```
## fileio.read
read(fd: number, buffer: ArrayBuffer, options: {
offset?: number;
length?: number;
position?: number;
}, callback: AsyncCallback&lt;ReadOut&gt;): void
read(fd: number, buffer: ArrayBuffer, options: { offset?: number; length?: number; position?: number; }, callback: AsyncCallback&lt;ReadOut&gt;): void
Reads data from a file. This API uses an asynchronous callback to return the result.
......@@ -732,11 +727,7 @@ Reads data from a file. This API uses an asynchronous callback to return the res
## fileio.readSync
readSync(fd: number, buffer: ArrayBuffer, options?: {
offset?: number;
length?: number;
position?: number;
}): number
readSync(fd: number, buffer: ArrayBuffer, options?: { offset?: number; length?: number; position?: number; }): number
Synchronously reads data from a file.
......@@ -790,10 +781,10 @@ Deletes a directory. This API uses a promise to return the result.
```js
let dirPath = pathDir + '/testDir';
fileio.rmdir(dirPath).then(function() {
fileio.rmdir(dirPath).then(function () {
console.info("Directory deleted");
}).catch(function(err){
console.info("Failed to delete the directory. Error:"+ err);
}).catch(function (err) {
console.info("rmdir failed with error:" + err);
});
```
......@@ -817,7 +808,7 @@ Deletes a directory. This API uses an asynchronous callback to return the result
```js
let dirPath = pathDir + '/testDir';
fileio.rmdir(dirPath, function(err){
fileio.rmdir(dirPath, function (err) {
// Do something.
console.info("Directory deleted");
});
......@@ -870,10 +861,10 @@ Deletes a file. This API uses a promise to return the result.
```js
let filePath = pathDir + "/test.txt";
fileio.unlink(filePath).then(function(){
fileio.unlink(filePath).then(function () {
console.info("File deleted");
}).catch(function(error){
console.info("Failed to delete the file. Error:"+ error);
}).catch(function (error) {
console.info("remove file failed with error:" + error);
});
```
......@@ -897,7 +888,7 @@ Deletes a file. This API uses an asynchronous callback to return the result.
```js
let filePath = pathDir + "/test.txt";
fileio.unlink(filePath, function(err) {
fileio.unlink(filePath, function (err) {
console.info("File deleted");
});
```
......@@ -927,12 +918,7 @@ Synchronously deletes a file.
## fileio.write
write(fd: number, buffer: ArrayBuffer | string, options?: {
offset?: number;
length?: number;
position?: number;
encoding?: string;
}): Promise&lt;number&gt;
write(fd: number, buffer: ArrayBuffer|string, options?: { offset?: number; length?: number; position?: number; encoding?: string; }): Promise&lt;number&gt;
Writes data into a file. This API uses a promise to return the result.
......@@ -943,7 +929,7 @@ Writes data into a file. This API uses a promise to return the result.
| Name | Type | Mandatory | Description |
| ------- | ------------------------------- | ---- | ---------------------------------------- |
| fd | number | Yes | File descriptor of the file to write. |
| buffer | ArrayBuffer&nbsp;\|&nbsp;string | Yes | Data to write. It can be a string or data from a buffer. |
| buffer | ArrayBuffer\|string | Yes | Data to write. It can be a string or data from a buffer. |
| options | Object | No | The options are as follows:<br>-&nbsp;**offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): start position to write the data in the file. By default, data is written from the current position.<br>-&nbsp;**encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.<br>Constraints: offset + length <= Buffer size|
**Return value**
......@@ -957,22 +943,17 @@ Writes data into a file. This API uses a promise to return the result.
```js
let filePath = pathDir + "/test.txt";
let fd = fileio.openSync(filePath, 0o100 | 0o2, 0o666);
fileio.write(fd, "hello, world").then(function(number){
console.info("Data written to the file. Size is:"+ number);
}).catch(function(err){
console.info("Failed to write data to the file. Error:"+ err);
fileio.write(fd, "hello, world").then(function (number) {
console.info("write data to file succeed and size is:" + number);
}).catch(function (err) {
console.info("write data to file failed with error:" + err);
});
```
## fileio.write
write(fd: number, buffer: ArrayBuffer | string, options: {
offset?: number;
length?: number;
position?: number;
encoding?: string;
}, callback: AsyncCallback&lt;number&gt;): void
write(fd: number, buffer: ArrayBuffer|string, options: { offset?: number; length?: number; position?: number; encoding?: string; }, callback: AsyncCallback&lt;number&gt;): void
Writes data into a file. This API uses an asynchronous callback to return the result.
......@@ -983,7 +964,7 @@ Writes data into a file. This API uses an asynchronous callback to return the re
| Name | Type | Mandatory | Description |
| -------- | ------------------------------- | ---- | ---------------------------------------- |
| fd | number | Yes | File descriptor of the file to write. |
| buffer | ArrayBuffer&nbsp;\|&nbsp;string | Yes | Data to write. It can be a string or data from a buffer. |
| buffer | ArrayBuffer\|string | Yes | Data to write. It can be a string or data from a buffer. |
| options | Object | No | The options are as follows:<br>-&nbsp;**offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): start position to write the data in the file. By default, data is written from the current position.<br>-&nbsp;**encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.<br>Constraints: offset + length <= Buffer size|
| callback | AsyncCallback&lt;number&gt; | Yes | Callback invoked when the data is written asynchronously. |
......@@ -994,7 +975,7 @@ Writes data into a file. This API uses an asynchronous callback to return the re
let fd = fileio.openSync(filePath, 0o100 | 0o2, 0o666);
fileio.write(fd, "hello, world", function (err, bytesWritten) {
if (bytesWritten) {
console.info("Data written to the file. Size is:"+ bytesWritten);
console.info("write data to file succeed and size is:" + bytesWritten);
}
});
```
......@@ -1002,12 +983,7 @@ Writes data into a file. This API uses an asynchronous callback to return the re
## fileio.writeSync
writeSync(fd: number, buffer: ArrayBuffer | string, options?: {
offset?: number;
length?: number;
position?: number;
encoding?: string;
}): number
writeSync(fd: number, buffer: ArrayBuffer|string, options?: { offset?: number; length?: number; position?: number; encoding?: string; }): number
Synchronously writes data into a file.
......@@ -1018,7 +994,7 @@ Synchronously writes data into a file.
| Name | Type | Mandatory | Description |
| ------- | ------------------------------- | ---- | ---------------------------------------- |
| fd | number | Yes | File descriptor of the file to write. |
| buffer | ArrayBuffer&nbsp;\|&nbsp;string | Yes | Data to write. It can be a string or data from a buffer. |
| buffer | ArrayBuffer\|string | Yes | Data to write. It can be a string or data from a buffer. |
| options | Object | No | The options are as follows:<br>-&nbsp;**offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): start position to write the data in the file. By default, data is written from the current position.<br>-&nbsp;**encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.<br>Constraints: offset + length <= Buffer size|
**Return value**
......@@ -1061,10 +1037,10 @@ Calculates the hash value of a file. This API uses a promise to return the resul
```js
let filePath = pathDir + "/test.txt";
fileio.hash(filePath, "sha256").then(function(str){
console.info("Calculated file hash:"+ str);
}).catch(function(err){
console.info("Failed to calculate the file hash. Error:"+ err);
fileio.hash(filePath, "sha256").then(function (str) {
console.info("calculate file hash succeed:" + str);
}).catch(function (err) {
console.info("calculate file hash failed with error:" + err);
});
```
......@@ -1089,9 +1065,9 @@ Calculates the hash value of a file. This API uses an asynchronous callback to r
```js
let filePath = pathDir + "/test.txt";
fileio.hash(filePath, "sha256", function(err, hashStr) {
fileio.hash(filePath, "sha256", function (err, hashStr) {
if (hashStr) {
console.info("Calculated file hash:"+ hashStr);
console.info("calculate file hash succeed:" + hashStr);
}
});
```
......@@ -1122,10 +1098,10 @@ Changes file permissions. This API uses a promise to return the result.
```js
let filePath = pathDir + "/test.txt";
fileio.chmod(filePath, 0o700).then(function() {
fileio.chmod(filePath, 0o700).then(function () {
console.info("File permissions changed");
}).catch(function(err){
console.info("Failed to change file permissions. Error:"+ err);
}).catch(function (err) {
console.info("chmod failed with error:" + err);
});
```
......@@ -1191,7 +1167,7 @@ Obtains file information based on the file descriptor. This API uses a promise t
| Name | Type | Mandatory | Description |
| ---- | ------ | ---- | ------------ |
| fd | number | Yes | File descriptor of the target file.|
| fd | number | Yes | Descriptor of the target file.|
**Return value**
......@@ -1204,10 +1180,10 @@ Obtains file information based on the file descriptor. This API uses a promise t
```js
let filePath = pathDir + "/test.txt";
let fd = fileio.openSync(filePath);
fileio.fstat(fd).then(function(stat){
console.info("Obtained file info:"+ JSON.stringify(stat));
}).catch(function(err){
console.info("Failed to obtain file info. Error:"+ err);
fileio.fstat(fd).then(function (stat) {
console.info("fstat succeed, the size of file is " + stat.size);
}).catch(function (err) {
console.info("fstat failed with error:" + err);
});
```
......@@ -1293,17 +1269,17 @@ Truncates a file based on the file descriptor. This API uses a promise to return
```js
let filePath = pathDir + "/test.txt";
let fd = fileio.openSync(filePath);
fileio.ftruncate(fd, 5).then(function(err) {
fileio.ftruncate(fd, 5).then(function (err) {
console.info("File truncated");
}).catch(function(err){
console.info("Failed to truncate the file. Error:"+ err);
}).catch(function (err) {
console.info("truncate file failed with error:" + err);
});
```
## fileio.ftruncate<sup>7+</sup>
ftruncate(fd: number, len: number, callback: AsyncCallback&lt;void&gt;): void
ftruncate(fd: number, len?: number, callback: AsyncCallback&lt;void&gt;): void
Truncates a file based on the file descriptor. This API uses an asynchronous callback to return the result.
......@@ -1323,7 +1299,7 @@ Truncates a file based on the file descriptor. This API uses an asynchronous cal
let filePath = pathDir + "/test.txt";
let fd = fileio.openSync(filePath);
let len = 5;
fileio.ftruncate(fd, 5, function(err){
fileio.ftruncate(fd, 5, function (err) {
// Do something.
});
```
......@@ -1366,7 +1342,7 @@ Truncates a file based on the file path. This API uses a promise to return the r
| Name| Type | Mandatory| Description |
| ------ | ------ | ---- | -------------------------------- |
| path | string | Yes | Application sandbox path of the file to truncate.|
| path | string | Yes | Application sandbox path of the file to truncate. |
| len | number | No | File length, in bytes, after truncation.|
**Return value**
......@@ -1380,17 +1356,17 @@ Truncates a file based on the file path. This API uses a promise to return the r
```js
let filePath = pathDir + "/test.txt";
let len = 5;
fileio.truncate(filePath, len).then(function(){
fileio.truncate(filePath, len).then(function () {
console.info("File truncated");
}).catch(function(err){
console.info("Failed to truncate the file. Error:"+ err);
}).catch(function (err) {
console.info("truncate file failed with error:" + err);
});
```
## fileio.truncate<sup>7+</sup>
truncate(path: string, len: number, callback: AsyncCallback&lt;void&gt;): void
truncate(path: string, len?: number, callback: AsyncCallback&lt;void&gt;): void
Truncates a file based on the file path. This API uses an asynchronous callback to return the result.
......@@ -1409,7 +1385,7 @@ Truncates a file based on the file path. This API uses an asynchronous callback
```js
let filePath = pathDir + "/test.txt";
let len = 5;
fileio.truncate(filePath, len, function(err){
fileio.truncate(filePath, len, function (err) {
// Do something.
});
```
......@@ -1441,11 +1417,7 @@ Synchronously truncates a file based on the file path.
## fileio.readText<sup>7+</sup>
readText(filePath: string, options?: {
position?: number;
length?: number;
encoding?: string;
}): Promise&lt;string&gt;
readText(filePath: string, options?: { position?: number; length?: number; encoding?: string; }): Promise&lt;string&gt;
Reads the text content of a file. This API uses a promise to return the result.
......@@ -1468,21 +1440,17 @@ Reads the text content of a file. This API uses a promise to return the result.
```js
let filePath = pathDir + "/test.txt";
fileio.readText(filePath).then(function(str) {
console.info("Read text successfully:"+ str);
}).catch(function(err){
console.info("Failed to read the text. Error:"+ err);
fileio.readText(filePath).then(function (str) {
console.info("readText succeed:" + str);
}).catch(function (err) {
console.info("readText failed with error:" + err);
});
```
## fileio.readText<sup>7+</sup>
readText(filePath: string, options: {
position?: number;
length?: number;
encoding?: string;
}, callback: AsyncCallback&lt;string&gt;): void
readText(filePath: string, options: { position?: number; length?: number; encoding?: string; }, callback: AsyncCallback&lt;string&gt;): void
Reads the text content of a file. This API uses an asynchronous callback to return the result.
......@@ -1500,7 +1468,7 @@ Reads the text content of a file. This API uses an asynchronous callback to retu
```js
let filePath = pathDir + "/test.txt";
fileio.readText(filePath, { position: 1, encoding: 'UTF-8' }, function(err, str){
fileio.readText(filePath, { position: 1, encoding: 'UTF-8' }, function (err, str) {
// Do something.
});
```
......@@ -1508,11 +1476,7 @@ Reads the text content of a file. This API uses an asynchronous callback to retu
## fileio.readTextSync<sup>7+</sup>
readTextSync(filePath: string, options?: {
position?: number;
length?: number;
encoding?: string;
}): string
readTextSync(filePath: string, options?: { position?: number; length?: number; encoding?: string; }): string
Synchronously reads the text of a file.
......@@ -1563,10 +1527,10 @@ Obtains link information. This API uses a promise to return the result.
```js
let filePath = pathDir + "/test.txt";
fileio.lstat(filePath).then(function(stat){
console.info("Get link info:"+ JSON.stringify(stat));
}).catch(function(err){
console.info("Failed to obtain link info. Error:"+ err);
fileio.lstat(filePath).then(function (stat) {
console.info("get link status succeed, the size of file is" + stat.size);
}).catch(function (err) {
console.info("get link status failed with error:" + err);
});
```
......@@ -1637,7 +1601,7 @@ Renames a file. This API uses a promise to return the result.
| Name | Type | Mandatory| Description |
| ------- | ------ | ---- | ---------------------------- |
| oldPath | string | Yes | Application sandbox path of the file to rename.|
| newPath | String | Yes | Application sandbox path of the file renamed. |
| newPath | string | Yes | Application sandbox path of the file renamed. |
**Return value**
......@@ -1650,10 +1614,10 @@ Renames a file. This API uses a promise to return the result.
```js
let srcFile = pathDir + "/test.txt";
let dstFile = pathDir + '/new.txt';
fileio.rename(srcFile, dstFile).then(function() {
fileio.rename(srcFile, dstFile).then(function () {
console.info("File renamed");
}).catch(function(err){
console.info("Failed to rename the file. Error:"+ err);
}).catch(function (err) {
console.info("rename failed with error:" + err);
});
```
......@@ -1671,15 +1635,15 @@ Renames a file. This API uses an asynchronous callback to return the result.
| Name | Type | Mandatory| Description |
| -------- | ------------------------- | ---- | ---------------------------- |
| oldPath | string | Yes | Application sandbox path of the file to rename.|
| newPath | String | Yes | Application sandbox path of the file renamed. |
| Callback | AsyncCallback&lt;void&gt; | Yes | Callback invoked when the file is asynchronously renamed. |
| newPath | string | Yes | Application sandbox path of the file renamed. |
| callback | AsyncCallback&lt;void&gt; | Yes | Callback invoked when the file is asynchronously renamed. |
**Example**
```js
let srcFile = pathDir + "/test.txt";
let dstFile = pathDir + '/new.txt';
fileio.rename(srcFile, dstFile, function(err){
fileio.rename(srcFile, dstFile, function (err) {
});
```
......@@ -1697,7 +1661,7 @@ Synchronously renames a file.
| Name | Type | Mandatory| Description |
| ------- | ------ | ---- | ---------------------------- |
| oldPath | string | Yes | Application sandbox path of the file to rename.|
| newPath | String | Yes | Application sandbox path of the file renamed. |
| newPath | string | Yes | Application sandbox path of the file renamed. |
**Example**
......@@ -1733,10 +1697,10 @@ Flushes data of a file to disk. This API uses a promise to return the result.
```js
let filePath = pathDir + "/test.txt";
let fd = fileio.openSync(filePath);
fileio.fsync(fd).then(function(){
fileio.fsync(fd).then(function () {
console.info("Data flushed");
}).catch(function(err){
console.info("Failed to flush data. Error:"+ err);
}).catch(function (err) {
console.info("sync data failed with error:" + err);
});
```
......@@ -1761,7 +1725,7 @@ Flushes data of a file to disk. This API uses an asynchronous callback to return
```js
let filePath = pathDir + "/test.txt";
let fd = fileio.openSync(filePath);
fileio.fsync(fd, function(err){
fileio.fsync(fd, function (err) {
// Do something.
});
```
......@@ -1815,10 +1779,10 @@ Flushes data of a file to disk. This API uses a promise to return the result. **
```js
let filePath = pathDir + "/test.txt";
let fd = fileio.openSync(filePath);
fileio.fdatasync(fd).then(function(err) {
fileio.fdatasync(fd).then(function (err) {
console.info("Data flushed");
}).catch(function(err){
console.info("Failed to flush data. Error:"+ err);
}).catch(function (err) {
console.info("sync data failed with error:" + err);
});
```
......@@ -1836,7 +1800,7 @@ Flushes data of a file to disk. This API uses an asynchronous callback to return
| Name | Type | Mandatory | Description |
| -------- | ------------------------------- | ---- | ----------------- |
| fd | number | Yes | File descriptor of the file to synchronize. |
| callback | AsyncCallback&nbsp;&lt;void&gt; | Yes | Callback invoked when the file data is synchronized in asynchronous mode.|
| callback | AsyncCallback&lt;void&gt; | Yes | Callback invoked when the file data is synchronized in asynchronous mode.|
**Example**
......@@ -1898,10 +1862,10 @@ Creates a symbolic link based on the file path. This API uses a promise to retur
```js
let srcFile = pathDir + "/test.txt";
let dstFile = pathDir + '/test';
fileio.symlink(srcFile, dstFile).then(function() {
fileio.symlink(srcFile, dstFile).then(function () {
console.info("Symbolic link created");
}).catch(function(err){
console.info("Failed to create the symbolic link. Error:"+ err);
}).catch(function (err) {
console.info("symlink failed with error:" + err);
});
```
......@@ -1984,10 +1948,10 @@ Changes the file owner based on the file path. This API uses a promise to return
```js
let filePath = pathDir + "/test.txt";
let stat = fileio.statSync(filePath);
fileio.chown(filePath, stat.uid, stat.gid).then(function(){
fileio.chown(filePath, stat.uid, stat.gid).then(function () {
console.info("File owner changed");
}).catch(function(err){
console.info("Failed to change the file owner. Error:"+ err);
}).catch(function (err) {
console.info("chown failed with error:" + err);
});
```
......@@ -2014,7 +1978,7 @@ Changes the file owner based on the file path. This API uses an asynchronous cal
```js
let filePath = pathDir + "/test.txt";
let stat = fileio.statSync(filePath)
fileio.chown(filePath, stat.uid, stat.gid, function (err){
fileio.chown(filePath, stat.uid, stat.gid, function (err) {
// Do something.
});
```
......@@ -2068,10 +2032,10 @@ Creates a temporary directory. This API uses a promise to return the result.
**Example**
```js
fileio.mkdtemp(pathDir + "/XXXXXX").then(function(pathDir){
console.info("mkdtemp succeed:"+ pathDir);
}).catch(function(err){
console.info("Failed to create the temporary directory. Error:"+ err);
fileio.mkdtemp(pathDir + "/XXXXXX").then(function (pathDir) {
console.info("mkdtemp succeed:" + pathDir);
}).catch(function (err) {
console.info("mkdtemp failed with error:" + err);
});
```
......@@ -2154,10 +2118,10 @@ Changes file permissions based on the file descriptor. This API uses a promise t
let filePath = pathDir + "/test.txt";
let fd = fileio.openSync(filePath);
let mode = 0o700;
fileio.fchmod(fd, mode).then(function() {
fileio.fchmod(fd, mode).then(function () {
console.info("File permissions changed");
}).catch(function(err){
console.info("Failed to change file permissions. Error:"+ err);
}).catch(function (err) {
console.info("chmod failed with error:" + err);
});
```
......@@ -2176,7 +2140,7 @@ Changes file permissions based on the file descriptor. This API uses an asynchro
| -------- | ------------------------------- | ---- | ---------------------------------------- |
| fd | number | Yes | File descriptor of the target file. |
| mode | number | Yes | Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (&#124;).<br>-&nbsp;**0o700**: The owner has the read, write, and execute permissions.<br>- &nbsp;**0o400**: The owner has the read permission.<br>-&nbsp;**0o200**: The owner has the write permission.<br>-&nbsp;**0o100**: The owner has the execute permission.<br>-&nbsp;**0o070**: The user group has the read, write, and execute permissions.<br>-&nbsp;**0o040**: The user group has the read permission.<br>-&nbsp;**0o020**: The user group has the write permission.<br>-&nbsp;**0o010**: The user group has the execute permission.<br>-&nbsp;**0o007**: Other users have the read, write, and execute permissions.<br>-&nbsp;**0o004**: Other users have the read permission.<br>-&nbsp;**0o002**: Other users have the write permission.<br>-&nbsp;**0o001**: Other users have the execute permission.|
| callback | AsyncCallback&nbsp;&lt;void&gt; | Yes | Callback invoked when the file permissions are changed asynchronously. |
| callback | AsyncCallback&lt;void&gt; | Yes | Callback invoked when the file permissions are changed asynchronously. |
**Example**
......@@ -2240,10 +2204,10 @@ Opens a file stream based on the file path. This API uses a promise to return th
```js
let filePath = pathDir + "/test.txt";
fileio.createStream(filePath, "r+").then(function(stream){
fileio.createStream(filePath, "r+").then(function (stream) {
console.info("Stream created");
}).catch(function(err){
console.info("Failed to create the stream. Error:"+ err);
}).catch(function (err) {
console.info("createStream failed with error:" + err);
});
```
......@@ -2268,7 +2232,7 @@ Opens a file stream based on the file path. This API uses an asynchronous callba
```js
let filePath = pathDir + "/test.txt";
fileio.createStream(filePath, "r+", function(err, stream){
fileio.createStream(filePath, "r+", function (err, stream) {
// Do something.
});
```
......@@ -2329,10 +2293,10 @@ Opens a file stream based on the file descriptor. This API uses a promise to ret
```js
let filePath = pathDir + "/test.txt";
let fd = fileio.openSync(filePath);
fileio.fdopenStream(fd, "r+").then(function(stream){
fileio.fdopenStream(fd, "r+").then(function (stream) {
console.info("Stream opened");
}).catch(function(err){
console.info("Failed to open the stream. Error:"+ err);
}).catch(function (err) {
console.info("openStream failed with error:" + err);
});
```
......@@ -2351,7 +2315,7 @@ Opens a file stream based on the file descriptor. This API uses an asynchronous
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| fd | number | Yes | File descriptor of the target file. |
| mode | string | Yes | -&nbsp;**r**: Open a file for reading. The file must exist.<br>-&nbsp;**r+**: Open a file for both reading and writing. The file must exist.<br>-&nbsp;**w**: Open a file for writing. If the file exists, clear its content. If the file does not exist, create a file.<br>-&nbsp;**w+**: Open a file for both reading and writing. If the file exists, clear its content. If the file does not exist, create a file.<br>-&nbsp;**a**: Open a file in append mode for writing at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).<br>-&nbsp;**a+**: Open a file in append mode for reading or updating at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).|
| callback | AsyncCallback&nbsp;&lt;[Stream](#stream)&gt; | Yes | Callback invoked when the stream is open asynchronously. |
| callback | AsyncCallback&lt;[Stream](#stream)&gt; | Yes | Callback invoked when the stream is open asynchronously. |
**Example**
......@@ -2422,10 +2386,10 @@ Changes the file owner based on the file descriptor. This API uses a promise to
let filePath = pathDir + "/test.txt";
let fd = fileio.openSync(filePath);
let stat = fileio.statSync(filePath);
fileio.fchown(fd, stat.uid, stat.gid).then(function() {
fileio.fchown(fd, stat.uid, stat.gid).then(function () {
console.info("File owner changed");
}).catch(function(err){
console.info("Failed to change the file owner. Error:"+ err);
}).catch(function (err) {
console.info("chown failed with error:" + err);
});
```
......@@ -2453,7 +2417,7 @@ Changes the file owner based on the file descriptor. This API uses an asynchrono
let filePath = pathDir + "/test.txt";
let fd = fileio.openSync(filePath);
let stat = fileio.statSync(filePath);
fileio.fchown(fd, stat.uid, stat.gid, function (err){
fileio.fchown(fd, stat.uid, stat.gid, function (err) {
// Do something.
});
```
......@@ -2512,10 +2476,10 @@ Changes the file owner (owner of the symbolic link, not the file referred to by
```js
let filePath = pathDir + "/test.txt";
let stat = fileio.statSync(filePath);
fileio.lchown(filePath, stat.uid, stat.gid).then(function() {
fileio.lchown(filePath, stat.uid, stat.gid).then(function () {
console.info("File owner changed");
}).catch(function(err){
console.info("Failed to change the file owner. Error:"+ err);
}).catch(function (err) {
console.info("chown failed with error:" + err);
});
```
......@@ -2542,7 +2506,7 @@ Changes the file owner (owner of the symbolic link, not the file referred to by
```js
let filePath = pathDir + "/test.txt";
let stat = fileio.statSync(filePath);
fileio.lchown(filePath, stat.uid, stat.gid, function (err){
fileio.lchown(filePath, stat.uid, stat.gid, function (err) {
// Do something.
});
```
......@@ -2586,8 +2550,8 @@ Listens for file or directory changes. This API uses an asynchronous callback to
| Name | Type | Mandatory| Description |
| -------- | --------------------------------- | ---- | ------------------------------------------------------------ |
| filePath | string | Yes | Application sandbox path of the file. |
| events | Number | Yes | -&nbsp;**1**: The file or directory is renamed.<br>-&nbsp;**2**: The file or directory is modified.<br>-&nbsp;**3**: The file or directory is modified and renamed.|
| callback | AsyncCallback&lt;number&nbsp;&gt; | Yes | Called each time a change is detected. |
| events | number | Yes | -&nbsp;**1**: The file or directory is renamed.<br>-&nbsp;**2**: The file or directory is modified.<br>-&nbsp;**3**: The file or directory is modified and renamed.|
| callback | AsyncCallback&lt;number&gt; | Yes | Called each time a change is detected. |
**Return value**
......@@ -2599,8 +2563,8 @@ Listens for file or directory changes. This API uses an asynchronous callback to
```js
let filePath = pathDir +"/test.txt";
fileio.createWatcher(filePath, 1, function(number){
console.info("Monitoring times: "+number);
fileio.createWatcher(filePath, 1, function (number) {
console.info("Monitoring times: " +number);
});
```
......@@ -2616,7 +2580,7 @@ Obtains the file read result. This class applies only to the **read()** method.
| --------- | ---------- | ---- | ---- | ----------------- |
| bytesRead | number | Yes | Yes | Length of the data read. |
| offset | number | Yes | Yes | Position of the buffer to which the data will be read in reference to the start address of the buffer.|
| buffer | ArrayBufer | Yes | Yes | Buffer for storing the data read. |
| buffer | ArrayBuffer | Yes | Yes | Buffer for storing the data read. |
## Stat
......@@ -2631,7 +2595,7 @@ Provides detailed file information. Before calling a method of the **Stat** clas
| ------ | ------ | ---- | ---- | ---------------------------------------- |
| dev | number | Yes | No | Major device number. |
| ino | number | Yes | No | File ID. Different files on the same device have different **ino**s. |
| mode | number | Yes | No | File type and permissions. The first four bits indicate the file type, and the last 12 bits indicate the permissions. The bit fields are described as follows:<br>-&nbsp;**0o170000**: mask used to obtain the file type.<br>-&nbsp;**0o140000**: The file is a socket.<br>-&nbsp;**0o120000**: The file is a symbolic link.<br>-&nbsp;**0o100000**: The file is a regular file.<br>-&nbsp;**0o060000**: The file is a block device.<br>-&nbsp;**0o040000**: The file is a directory.<br>-&nbsp;**0o020000**: The file is a character device.<br>-&nbsp;**0o010000**: The file is a named pipe (FIFO).<br>-&nbsp;**0o0700**: mask used to obtain the owner permissions.<br>-&nbsp;**0o0400**: The owner has the permission to read a regular file or a directory entry.<br>-&nbsp;**0o0200**: The owner has the permission to write a regular file or create and delete a directory entry.<br>-&nbsp;**0o0100**: The owner has the permission to execute a regular file or search for the specified path in a directory.<br>-&nbsp;**0o0070**: mask used to obtain the user group permissions.<br>-&nbsp;**0o0040**: The user group has the permission to read a regular file or a directory entry.<br>-&nbsp;**0o0020**: The user group has the permission to write a regular file or create and delete a directory entry.<br>-&nbsp;**0o0010**: The user group has the permission to execute a regular file or search for the specified path in a directory.<br>-&nbsp;**0o0007**: mask used to obtain the permissions of other users.<br>-&nbsp;**0o0004**: Other users have the permission to read a regular file or a directory entry.<br>-&nbsp;**0o0002**: Other users have the permission to write a regular file or create and delete a directory entry.<br>-&nbsp;**0o0001**: Other users have the permission to execute a regular file or search for the specified path in a directory.|
| mode | number | Yes | No | File type and permissions. The first four bits indicate the file type, and the last 12 bits indicate the permissions. The bit fields are described as follows:<br>-&nbsp;**0o170000**: mask used to obtain the file type.<br>-&nbsp;**0o140000**: The file is a socket.<br>-&nbsp;**0o120000**: The file is a symbolic link.<br>-&nbsp;**0o100000**: The file is a regular file.<br>-&nbsp;**0o060000**: The file is a block device.<br>-&nbsp;**0o040000**: The file is a directory.<br>-&nbsp;**0o020000**: The file is a character device.<br>- **0o010000**: The file is a named pipe (FIFO).<br>-&nbsp;**0o0700**: mask used to obtain the owner permissions.<br>-&nbsp;**0o0400**: The owner has the permission to read a regular file or a directory entry.<br>-&nbsp;**0o0200**: The owner has the permission to write a regular file or create and delete a directory entry.<br>-&nbsp;**0o0100**: The owner has the permission to execute a regular file or search for the specified path in a directory.<br>-&nbsp;**0o0070**: mask used to obtain the user group permissions.<br>-&nbsp;**0o0040**: The user group has the permission to read a regular file or a directory entry.<br>-&nbsp;**0o0020**: The user group has the permission to write a regular file or create and delete a directory entry.<br>-&nbsp;**0o0010**: The user group has the permission to execute a regular file or search for the specified path in a directory.<br>-&nbsp;**0o0007**: mask used to obtain the permissions of other users.<br>-&nbsp;**0o0004**: Other users have the permission to read a regular file or a directory entry.<br>-&nbsp;**0o0002**: Other users have the permission to write a regular file or create and delete a directory entry.<br>-&nbsp;**0o0001**: Other users have the permission to execute a regular file or search for the specified path in a directory.|
| nlink | number | Yes | No | Number of hard links in the file. |
| uid | number | Yes | No | User ID, that is ID of the file owner. |
| gid | number | Yes | No | Group ID, that is, ID of the user group of the file. |
......@@ -2814,10 +2778,10 @@ Stops the **watcher** instance. This API uses a promise to return the result.
```js
let filePath = path + "/test.txt";
let watcher = fileio.createWatcher(filePath, 1, function(number){
console.info("Monitoring times: "+number);
let watcher = fileio.createWatcher(filePath, 1, function (number) {
console.info("Monitoring times: " +number);
});
watcher.stop().then(function(){
watcher.stop().then(function () {
console.info("Watcher stopped");
});
```
......@@ -2841,10 +2805,10 @@ Stops the **watcher** instance. This API uses an asynchronous callback to return
```js
let filePath = path +"/test.txt";
let watcher = fileio.createWatcher(filePath, 1, function(number){
console.info("Monitoring times: "+number);
let watcher = fileio.createWatcher(filePath, 1, function (number) {
console.info("Monitoring times: " +number);
});
watcher.stop(function(){
watcher.stop(function () {
console.info("Watcher stopped");
})
```
......@@ -2874,10 +2838,10 @@ Closes the stream. This API uses a promise to return the result.
```js
let filePath = pathDir + "/test.txt";
let ss= fileio.createStreamSync(filePath, "r+");
ss.close().then(function(){
ss.close().then(function () {
console.info("File stream closed");
}).catch(function(err){
console.info("Failed to close the file stream. Error:"+ err);
}).catch(function (err) {
console.info("close fileStream failed with error:" + err);
});
```
......@@ -2943,10 +2907,10 @@ Flushes the stream. This API uses a promise to return the result.
```js
let filePath = pathDir + "/test.txt";
let ss= fileio.createStreamSync(filePath, "r+");
ss.flush().then(function (){
ss.flush().then(function () {
console.info("Stream flushed");
}).catch(function(err){
console.info("Failed to flush the stream. Error:"+ err);
}).catch(function (err) {
console.info("flush failed with error:" + err);
});
```
......@@ -2995,12 +2959,7 @@ Synchronously flushes the stream.
### write<sup>7+</sup>
write(buffer: ArrayBuffer | string, options?: {
offset?: number;
length?: number;
position?: number;
encoding?: string;
}): Promise&lt;number&gt;
write(buffer: ArrayBuffer|string, options?: { offset?: number; length?: number; position?: number; encoding?: string; }): Promise&lt;number&gt;
Writes data into the stream. This API uses a promise to return the result.
......@@ -3010,7 +2969,7 @@ Writes data into the stream. This API uses a promise to return the result.
| Name | Type | Mandatory | Description |
| ------- | ------------------------------- | ---- | ---------------------------------------- |
| buffer | ArrayBuffer&nbsp;\|&nbsp;string | Yes | Data to write. It can be a string or data from a buffer. |
| buffer | ArrayBuffer\|string | Yes | Data to write. It can be a string or data from a buffer. |
| options | Object | No | The options are as follows:<br>-&nbsp;**offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): start position to write the data in the file. By default, data is written from the current position.<br>-&nbsp;**encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.<br>Constraints: offset + length <= Buffer size |
**Return value**
......@@ -3024,22 +2983,17 @@ Writes data into the stream. This API uses a promise to return the result.
```js
let filePath = pathDir + "/test.txt";
let ss= fileio.createStreamSync(filePath, "r+");
ss.write("hello, world",{offset: 1,length: 5,position: 5,encoding :'utf-8'}).then(function (number){
console.info("Data written to the stream. Size is:"+ number);
}).catch(function(err){
console.info("Failed to write data to the stream. Error:"+ err);
ss.write("hello, world",{offset: 1,length: 5,position: 5,encoding :'utf-8'}).then(function (number) {
console.info("write succeed and size is:" + number);
}).catch(function (err) {
console.info("write failed with error:" + err);
});
```
### write<sup>7+</sup>
write(buffer: ArrayBuffer | string, options: {
offset?: number;
length?: number;
position?: number;
encoding?: string;
}, callback: AsyncCallback&lt;number&gt;): void
write(buffer: ArrayBuffer|string, options: { offset?: number; length?: number; position?: number; encoding?: string; }, callback: AsyncCallback&lt;number&gt;): void
Writes data into the stream. This API uses an asynchronous callback to return the result.
......@@ -3049,7 +3003,7 @@ Writes data into the stream. This API uses an asynchronous callback to return th
| Name | Type | Mandatory| Description |
| -------- | ------------------------------- | ---- | ------------------------------------------------------------ |
| buffer | ArrayBuffer&nbsp;\|&nbsp;string | Yes | Data to write. It can be a string or data from a buffer. |
| buffer | ArrayBuffer\|string | Yes | Data to write. It can be a string or data from a buffer. |
| options | Object | No | The options are as follows:<br>-&nbsp;**offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): start position to write the data in the file. By default, data is written from the current position.<br>-&nbsp;**encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.<br>Constraints: offset + length <= Buffer size|
| callback | AsyncCallback&lt;number&gt; | Yes | Callback invoked when the data is written asynchronously. |
......@@ -3061,7 +3015,7 @@ Writes data into the stream. This API uses an asynchronous callback to return th
ss.write("hello, world", {offset: 1, length: 5, position: 5, encoding :'utf-8'}, function (err, bytesWritten) {
if (bytesWritten) {
// Do something.
console.info("Data written to the stream. Size is:"+ bytesWritten);
console.info("write succeed and size is:" + bytesWritten);
}
});
```
......@@ -3069,12 +3023,7 @@ Writes data into the stream. This API uses an asynchronous callback to return th
### writeSync<sup>7+</sup>
writeSync(buffer: ArrayBuffer | string, options?: {
offset?: number;
length?: number;
position?: number;
encoding?: string;
}): number
writeSync(buffer: ArrayBuffer|string, options?: { offset?: number; length?: number; position?: number; encoding?: string; }): number
Synchronously writes data into the stream.
......@@ -3084,7 +3033,7 @@ Synchronously writes data into the stream.
| Name | Type | Mandatory | Description |
| ------- | ------------------------------- | ---- | ---------------------------------------- |
| buffer | ArrayBuffer&nbsp;\|&nbsp;string | Yes | Data to write. It can be a string or data from a buffer. |
| buffer | ArrayBuffer\|string | Yes | Data to write. It can be a string or data from a buffer. |
| options | Object | No | The options are as follows:<br>-&nbsp;**offset** (number): position of the data to write in reference to the start address of the data. The default value is **0**.<br>-&nbsp;**length** (number): length of the data to write. The default value is the buffer length minus the offset.<br>-&nbsp;**position** (number): start position to write the data in the file. By default, data is written from the current position.<br>-&nbsp;**encoding** (string): format of the string to be encoded. The default value is **utf-8**, which is the only value supported.<br>Constraints: offset + length <= Buffer size |
**Return value**
......@@ -3104,11 +3053,7 @@ Synchronously writes data into the stream.
### read<sup>7+</sup>
read(buffer: ArrayBuffer, options?: {
position?: number;
offset?: number;
length?: number;
}): Promise&lt;ReadOut&gt;
read(buffer: ArrayBuffer, options?: { position?: number; offset?: number; length?: number; }): Promise&lt;ReadOut&gt;
Reads data from the stream. This API uses a promise to return the result.
......@@ -3132,22 +3077,18 @@ Reads data from the stream. This API uses a promise to return the result.
```js
let filePath = pathDir + "/test.txt";
let ss = fileio.createStreamSync(filePath, "r+");
ss.read(new ArrayBuffer(4096), {offset: 1, length: 5, position: 5}).then(function (readOut){
ss.read(new ArrayBuffer(4096), {offset: 1, length: 5, position: 5}).then(function (readOut) {
console.info("Read data successfully");
console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
}).catch(function(err){
console.info("Failed to read data. Error:"+ err);
}).catch(function (err) {
console.info("read data failed with error:" + err);
});
```
### read<sup>7+</sup>
read(buffer: ArrayBuffer, options: {
position?: number;
offset?: number;
length?: number;
}, callback: AsyncCallback&lt;ReadOut&gt;): void
read(buffer: ArrayBuffer, options: { position?: number; offset?: number; length?: number; }, callback: AsyncCallback&lt;ReadOut&gt;): void
Reads data from the stream. This API uses an asynchronous callback to return the result.
......@@ -3177,11 +3118,7 @@ Reads data from the stream. This API uses an asynchronous callback to return the
### readSync<sup>7+</sup>
readSync(buffer: ArrayBuffer, options?: {
position?: number;
offset?: number;
length?: number;
}): number
readSync(buffer: ArrayBuffer, options?: { position?: number; offset?: number; length?: number; }): number
Synchronously reads data from the stream.
......@@ -3231,10 +3168,10 @@ Reads the next directory entry. This API uses a promise to return the result.
**Example**
```js
dir.read().then(function (dirent){
console.log("Read the next directory entry:"+JSON.stringify(dirent));
}).catch(function(err){
console.info("Failed to read the next directory entry. Error:"+ err);
dir.read().then(function (dirent) {
console.log("read succeed, the name of dirent is " + dirent.name);
}).catch(function (err) {
console.info("read failed with error:" + err);
});
```
......@@ -3259,7 +3196,7 @@ Reads the next directory entry. This API uses an asynchronous callback to return
dir.read(function (err, dirent) {
if (dirent) {
// Do something.
console.log("Read the next directory entry:"+JSON.stringify(dirent));
console.log("read succeed, the name of file is " + dirent.name);
}
});
```
......@@ -3297,7 +3234,7 @@ Closes a directory. This API uses a promise to return the result. After a direct
**Example**
```js
dir.close().then(function(err){
dir.close().then(function (err) {
console.info("close dir successfully");
});
```
......@@ -3314,7 +3251,7 @@ Closes a directory. This API uses an asynchronous callback to return the result.
**Example**
```js
dir.close(function(err){
dir.close(function (err) {
console.info("close dir successfully");
});
```
......@@ -3503,18 +3440,19 @@ Checks whether a directory entry is a symbolic link.
## Filter<sup>9+</sup>
Defines the file filter configuration.
**System API**: This is a system API.
**System capability**: SystemCapability.FileManagement.File.FileIO
Defines the file filter configuration.
| Name | Type | Description |
| ----------- | --------------- | ------------------ |
| suffix | Array&lt;string&gt; | File name extensions. The keywords in the array are of the OR relationship. |
| displayName | Array&lt;string&gt; | File name for fuzzy match. The keywords in the array are of the OR relationship.|
| displayName | Array&lt;string&gt; | File names for fuzzy match. The keywords in the array are of the OR relationship.|
| mimeType | Array&lt;string&gt; | MIME types to match. The keywords in the array are of the OR relationship. |
| fileSizeOver | number | File size to match. The files which are of the same or a lager size are matched. |
| lastModifiedAfter | Date | File modification time to match. The files modified after the specified time are matched. |
| excludeMedia | Boolean | Whether to exclude the files already in Media. |
<!--no_check-->
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册