提交 214216bf 编写于 作者: A Annie_wang

update docs

Signed-off-by: NAnnie_wang <annie.wangli@huawei.com>
上级 a3828280
...@@ -17,15 +17,31 @@ import fileio from '@ohos.fileio'; ...@@ -17,15 +17,31 @@ import fileio from '@ohos.fileio';
Before using the APIs provided by this module to perform operations on files or directories, obtain the path of the application sandbox as follows: Before using the APIs provided by this module to perform operations on files or directories, obtain the path of the application sandbox as follows:
Stage Model
```js
import Ability from '@ohos.application.Ability';
class MainAbility extends Ability {
onWindowStageCreate(windowStage) {
let context = this.context;
let path = context.filesDir;
}
}
```
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).
FA Model
```js ```js
import featureAbility from '@ohos.ability.featureAbility'; import featureAbility from '@ohos.ability.featureAbility';
let context = featureAbility.getContext(); let context = featureAbility.getContext();
let path = '';
context.getFilesDir().then((data) => { context.getFilesDir().then((data) => {
path = data; let path = 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).
## fileio.stat ## fileio.stat
...@@ -654,7 +670,7 @@ Reads data from a file. This API uses a promise to return the result. ...@@ -654,7 +670,7 @@ Reads data from a file. This API uses a promise to return the result.
let fd = fileio.openSync(path, 0o2); let fd = fileio.openSync(path, 0o2);
let buf = new ArrayBuffer(4096); let buf = new ArrayBuffer(4096);
fileio.read(fd, buf).then(function(readOut){ fileio.read(fd, buf).then(function(readOut){
console.info("Read file data"); console.info("Read file data successfully");
console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer))); console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
}).catch(function(err){ }).catch(function(err){
console.info("Failed to read file data. Error:"+ err); console.info("Failed to read file data. Error:"+ err);
...@@ -690,7 +706,7 @@ Reads data from a file. This API uses an asynchronous callback to return the res ...@@ -690,7 +706,7 @@ Reads data from a file. This API uses an asynchronous callback to return the res
let buf = new ArrayBuffer(4096); let buf = new ArrayBuffer(4096);
fileio.read(fd, buf, function (err, readOut) { fileio.read(fd, buf, function (err, readOut) {
if (readOut) { if (readOut) {
console.info("Read file data"); console.info("Read file data successfully");
console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer))); console.log(String.fromCharCode.apply(null, new Uint8Array(readOut.buffer)));
} }
}); });
...@@ -1263,7 +1279,7 @@ Truncates a file based on the file descriptor. This API uses an asynchronous cal ...@@ -1263,7 +1279,7 @@ Truncates a file based on the file descriptor. This API uses an asynchronous cal
| -------- | ------------------------- | ---- | ---------------- | | -------- | ------------------------- | ---- | ---------------- |
| fd | number | Yes | File descriptor of the file to truncate. | | fd | number | Yes | File descriptor of the file to truncate. |
| len | number | Yes | File length, in bytes, after truncation.| | len | number | Yes | File length, in bytes, after truncation.|
| callback | AsyncCallback&lt;void&gt; | Yes | Callback that returns no value. | | callback | AsyncCallback&lt;void&gt; | Yes | Callback that returns no value.|
**Example** **Example**
...@@ -1347,7 +1363,7 @@ Truncates a file based on the file path. This API uses an asynchronous callback ...@@ -1347,7 +1363,7 @@ Truncates a file based on the file path. This API uses an asynchronous callback
| -------- | ------------------------- | ---- | -------------------------------- | | -------- | ------------------------- | ---- | -------------------------------- |
| path | string | Yes | Application sandbox path of the file to truncate.| | path | string | Yes | Application sandbox path of the file to truncate.|
| len | number | Yes | File length, in bytes, after truncation.| | len | number | Yes | File length, in bytes, after truncation.|
| callback | AsyncCallback&lt;void&gt; | Yes | Callback that returns no value. | | callback | AsyncCallback&lt;void&gt; | Yes | Callback that returns no value.|
**Example** **Example**
...@@ -1411,7 +1427,7 @@ Reads the text content of a file. This API uses a promise to return the result. ...@@ -1411,7 +1427,7 @@ Reads the text content of a file. This API uses a promise to return the result.
```js ```js
fileio.readText(path).then(function(str) { fileio.readText(path).then(function(str) {
console.info("Read text:"+ str); console.info("Read text successfully:"+ str);
}).catch(function(err){ }).catch(function(err){
console.info("Failed to read the text. Error:"+ err); console.info("Failed to read the text. Error:"+ err);
}); });
...@@ -2159,7 +2175,7 @@ Opens a file stream based on the file path. This API uses a promise to return th ...@@ -2159,7 +2175,7 @@ Opens a file stream based on the file path. This API uses a promise to return th
| Type | Description | | Type | Description |
| --------------------------------- | --------- | | --------------------------------- | --------- |
| Promise&lt;[Stream](#stream7)&gt; | Promise used to return the result.| | Promise&lt;[Stream](#stream)&gt; | Promise used to return the result.|
**Example** **Example**
...@@ -2186,7 +2202,7 @@ Opens a file stream based on the file path. This API uses an asynchronous callba ...@@ -2186,7 +2202,7 @@ Opens a file stream based on the file path. This API uses an asynchronous callba
| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ | | -------- | --------------------------------------- | ---- | ------------------------------------------------------------ |
| path | string | Yes | Application sandbox path of the file. | | path | string | Yes | Application sandbox path of the 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).| | 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&lt;[Stream](#stream7)&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** **Example**
...@@ -2216,7 +2232,7 @@ Synchronously opens a stream based on the file path. ...@@ -2216,7 +2232,7 @@ Synchronously opens a stream based on the file path.
| Type | Description | | Type | Description |
| ------------------ | --------- | | ------------------ | --------- |
| [Stream](#stream7) | Stream opened.| | [Stream](#stream) | Stream opened.|
**Example** **Example**
...@@ -2244,7 +2260,7 @@ Opens a file stream based on the file descriptor. This API uses a promise to ret ...@@ -2244,7 +2260,7 @@ Opens a file stream based on the file descriptor. This API uses a promise to ret
| Type | Description | | Type | Description |
| --------------------------------- | --------- | | --------------------------------- | --------- |
| Promise&lt;[Stream](#stream7)&gt; | Promise used to return the result.| | Promise&lt;[Stream](#stream)&gt; | Promise used to return the result.|
**Example** **Example**
...@@ -2272,7 +2288,7 @@ Opens a file stream based on the file descriptor. This API uses an asynchronous ...@@ -2272,7 +2288,7 @@ Opens a file stream based on the file descriptor. This API uses an asynchronous
| -------- | ---------------------------------------- | ---- | ---------------------------------------- | | -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| fd | number | Yes | File descriptor of the target file. | | 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).| | 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](#stream7)&gt; | Yes | Callback invoked when the stream is open asynchronously. | | callback | AsyncCallback&nbsp;&lt;[Stream](#stream)&gt; | Yes | Callback invoked when the stream is open asynchronously. |
**Example** **Example**
...@@ -2303,7 +2319,7 @@ Synchronously opens a stream based on the file descriptor. ...@@ -2303,7 +2319,7 @@ Synchronously opens a stream based on the file descriptor.
| Type | Description | | Type | Description |
| ------------------ | --------- | | ------------------ | --------- |
| [Stream](#stream7) | Stream opened.| | [Stream](#stream) | Stream opened.|
**Example** **Example**
...@@ -2720,7 +2736,7 @@ Stops the **watcher** instance. This API uses a promise to return the result. ...@@ -2720,7 +2736,7 @@ Stops the **watcher** instance. This API uses a promise to return the result.
```js ```js
let filename = path +"/test.txt"; let filename = path +"/test.txt";
let watcher = await fileio.createWatcher(filename, 1, function(number){ let watcher = fileio.createWatcher(filename, 1, function(number){
console.info("Monitoring times: "+number); console.info("Monitoring times: "+number);
}); });
watcher.stop().then(function(){ watcher.stop().then(function(){
...@@ -2747,7 +2763,7 @@ Stops the **watcher** instance. This API uses an asynchronous callback to return ...@@ -2747,7 +2763,7 @@ Stops the **watcher** instance. This API uses an asynchronous callback to return
```js ```js
let filename = path +"/test.txt"; let filename = path +"/test.txt";
let watcher = await fileio.createWatcher(filename, 1, function(number){ let watcher = fileio.createWatcher(filename, 1, function(number){
console.info("Monitoring times: "+number); console.info("Monitoring times: "+number);
}); });
watcher.stop(function(){ watcher.stop(function(){
...@@ -3124,7 +3140,6 @@ Reads the next directory entry. This API uses a promise to return the result. ...@@ -3124,7 +3140,6 @@ Reads the next directory entry. This API uses a promise to return the result.
**Example** **Example**
```js ```js
let dir = fileio.opendirSync(path);
dir.read().then(function (dirent){ dir.read().then(function (dirent){
console.log("Read the next directory entry:"+JSON.stringify(dirent)); console.log("Read the next directory entry:"+JSON.stringify(dirent));
}).catch(function(err){ }).catch(function(err){
...@@ -3150,7 +3165,6 @@ Reads the next directory entry. This API uses an asynchronous callback to return ...@@ -3150,7 +3165,6 @@ Reads the next directory entry. This API uses an asynchronous callback to return
**Example** **Example**
```js ```js
let dir = fileio.opendirSync(path);
dir.read(function (err, dirent) { dir.read(function (err, dirent) {
if (dirent) { if (dirent) {
// Do something // Do something
...@@ -3177,7 +3191,6 @@ Synchronously reads the next directory entry. ...@@ -3177,7 +3191,6 @@ Synchronously reads the next directory entry.
**Example** **Example**
```js ```js
let dir = fileio.opendirSync(path);
let dirent = dir.readSync(); let dirent = dir.readSync();
``` ```
...@@ -3193,7 +3206,6 @@ Closes a directory. This API uses a promise to return the result. After a direct ...@@ -3193,7 +3206,6 @@ Closes a directory. This API uses a promise to return the result. After a direct
**Example** **Example**
```js ```js
let dir = fileio.opendirSync(path);
dir.close().then(function(err){ dir.close().then(function(err){
console.info("close dir successfully"); console.info("close dir successfully");
}); });
...@@ -3211,7 +3223,6 @@ Closes a directory. This API uses an asynchronous callback to return the result. ...@@ -3211,7 +3223,6 @@ Closes a directory. This API uses an asynchronous callback to return the result.
**Example** **Example**
```js ```js
let dir = fileio.opendirSync(path);
dir.close(function(err){ dir.close(function(err){
console.info("close dir successfully"); console.info("close dir successfully");
}); });
...@@ -3229,7 +3240,6 @@ Closes a directory. After a directory is closed, the file descriptor in Dir will ...@@ -3229,7 +3240,6 @@ Closes a directory. After a directory is closed, the file descriptor in Dir will
**Example** **Example**
```js ```js
let dir = fileio.opendirSync(path);
dir.closeSync(); dir.closeSync();
``` ```
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册