未验证 提交 80cdd988 编写于 作者: O openharmony_ci 提交者: Gitee

!23993 [翻译完成】#I7VDRT

Merge pull request !23993 from Annie_wang/PR22445
......@@ -40,6 +40,70 @@ export default class EntryAbility extends UIAbility {
For details about how to obtain the FA model context, see [Context](js-apis-inner-app-context.md#context).
## FileUri<sup>10+</sup>
### Attributes
**System capability**: SystemCapability.FileManagement.AppFileService
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| path<sup>10+</sup> | string | Yes| No| Path of the file corresponding to the URI.|
| name<sup>10+</sup> | string | Yes| No| Name of the file.|
### constructor<sup>10+</sup>
constructor(uriOrPath: string)
A constructor used to create a **FileUri** instance.
**System capability**: SystemCapability.FileManagement.AppFileService
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| uriOrPath | string | Yes| URI or path. The following types of URIs are available:<br>- Application sandbox URI: **file://\<bundleName>/\<sandboxPath>**<br>- URI of the user's document: **file://docs/storage/Users/currentUser/\<publicPath>**<br>- URI of the user's media asset: **file://media/\<mediaType>/IMG_DATATIME_ID/\<displayName>**|
**Error codes**
For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
| ID | Error Message |
| ---------------------------- | ---------- |
| 13900005 | I/O error |
| 13900042 | Unknown error |
**Example**
```js
let path = pathDir + '/test';
let uri = fileuri.getUriFromPath(filePath); // file://<packageName>/data/storage/el2/base/haps/entry/files/test
let fileUriObject = new fileuri.FileUri(uri);
console.info("The name of FileUri is " + fileUriObject.name);
```
### toString<sup>10+</sup>
toString(): string
**System capability**: SystemCapability.FileManagement.AppFileService
Obtains the URI of the string type.
**Return value**
| Type| Description|
| -------- | -------- |
| string | URI of the string type obtained.|
**Example**
```js
let path = pathDir + '/test';
let fileUriObject = new fileuri.FileUri(path);
console.info("The uri of FileUri is " + fileUriObject.toString());
```
## fileUri.getUriFromPath
getUriFromPath(path: string): string
......
......@@ -314,6 +314,7 @@ For details about the error codes, see [Basic File IO Error Codes](../errorcodes
console.info("close file failed with error message: " + err.message + ", error code: " + err.code);
} else {
console.info("close file success");
fs.closeSync(file);
}
});
```
......@@ -460,7 +461,7 @@ Copies a directory to the specified directory. This API uses a promise to return
| ------ | ------ | ---- | --------------------------- |
| src | string | Yes | Application sandbox path of the directory to copy.|
| dest | string | Yes | Application sandbox path of the destination directory.|
| mode | number | No | Copy mode. The default value is **0**.<br>- **0**: Throw an exception if a file conflict occurs.<br> Throw an exception if there is a directory with the same name in the destination directory and files with the same name exist in the conflicting directory. All the non-conflicting files in the source directory will be moved to the destination directory, and the non-conflicting files in the destination directory will be retained. The **data** attribute in the error returned provides information about the conflicting files in the Array\<[ConflictFiles](#conflictfiles)> format.<br>- **1**: Forcibly overwrite the files with the same name in the destination directory.<br>If there is a directory with the same name in the destination directory and files with the same name exist in the conflicting directory, all the files with the same name in the destination directory will be overwritten and the non-conflicting files will be retained.|
| mode | number | No | Copy mode. The default value is **0**.<br>- **0**: Throw an exception if a file conflict occurs.<br>Throw an exception if there is a directory with the same name in the destination directory and files with the same name exist in the conflicting directory. All the non-conflicting files in the source directory will be moved to the destination directory, and the non-conflicting files in the destination directory will be retained. The **data** attribute in the error returned provides information about the conflicting files in the Array\<[ConflictFiles](#conflictfiles10)> format.<br>- **1**: Forcibly overwrite the files with the same name in the destination directory. If there is a directory with the same name in the destination directory and files with the same name exist in the conflicting directory, all the files with the same name in the destination directory will be overwritten and the non-conflicting files will be retained.|
**Return value**
......@@ -494,7 +495,7 @@ For details about the error codes, see [Basic File IO Error Codes](../errorcodes
## fs.copyDir<sup>10+</sup>
copyDir(src: string, dest: string, mode?: number, callback: AsyncCallback\<void>): void
copyDir(src: string, dest: string, mode?: number, callback: AsyncCallback\<void, Array\<ConflictFiles>>): void
Copies a directory to the specified directory. This API uses an asynchronous callback to return the result.
......@@ -506,8 +507,8 @@ Copies a directory to the specified directory. This API uses an asynchronous cal
| ------ | ------ | ---- | --------------------------- |
| src | string | Yes | Application sandbox path of the directory to copy.|
| dest | string | Yes | Application sandbox path of the destination directory.|
| mode | number | No | Copy mode. The default value is **0**.<br>- **0**: Throw an exception if a file conflict occurs.<br> Throw an exception if there is a directory with the same name in the destination directory and files with the same name exist in the conflicting directory. All the non-conflicting files in the source directory will be moved to the destination directory, and the non-conflicting files in the destination directory will be retained. The **data** attribute in the error returned provides information about the conflicting files in the Array\<[ConflictFiles](#conflictfiles)> format.<br>- **1**: Forcibly overwrite the files with the same name in the destination directory.<br>If there is a directory with the same name in the destination directory and files with the same name exist in the conflicting directory, all the files with the same name in the destination directory will be overwritten and the non-conflicting files will be retained.|
| callback | AsyncCallback&lt;void&gt; | Yes | Callback invoked immediately after the directory is copied. |
| mode | number | No | Copy mode. The default value is **0**.<br>- **0**: Throw an exception if a file conflict occurs.<br>Throw an exception if there is a directory with the same name in the destination directory and files with the same name exist in the conflicting directory. All the non-conflicting files in the source directory will be moved to the destination directory, and the non-conflicting files in the destination directory will be retained. The **data** attribute in the error returned provides information about the conflicting files in the Array\<[ConflictFiles](#conflictfiles10)> format.<br>- **1**: Forcibly overwrite the files with the same name in the destination directory. If there is a directory with the same name in the destination directory and files with the same name exist in the conflicting directory, all the files with the same name in the destination directory will be overwritten and the non-conflicting files will be retained.|
| callback | AsyncCallback&lt;void, Array&lt;[ConflictFiles](#conflictfiles10)&gt;&gt; | Yes | Callback invoked immediately after the directory is copied. |
**Error codes**
......@@ -533,6 +534,41 @@ For details about the error codes, see [Basic File IO Error Codes](../errorcodes
});
```
## fs.dup<sup>10+</sup>
dup(fd: number): File
Opens a **File** object based on the specified FD.
**System capability**: SystemCapability.FileManagement.File.FileIO
**Parameters**
| Name | Type | Mandatory | Description |
| ------ | ------ | ---- | --------------------------- |
| fd | number | Yes | FD of the file.|
**Return value**
| Type | Description |
| ------------------- | ---------------------------- |
| [File](#file) | File object opened.|
**Error codes**
For details about the error codes, see [Basic File IO Error Codes](../errorcodes/errorcode-filemanagement.md#basic-file-io-error-codes).
**Example**
```js
// convert fd to file
let fd = 0; // fd comes from other modules
let file = fs.dup(fd);
console.info("The name of the file is " + file.name);
fs.closeSync(file);
```
## fs.mkdir
mkdir(path: string): Promise&lt;void&gt;
......@@ -644,7 +680,7 @@ Opens a file. This API uses a promise to return the result. File uniform resourc
| Type | Description |
| --------------------- | ----------- |
| Promise&lt;[File](#file)&gt; | Promise used to return the file object.|
| Promise&lt;[File](#file)&gt; | Promise used to return the **File** object.|
**Error codes**
......@@ -1566,6 +1602,7 @@ For details about the error codes, see [Basic File IO Error Codes](../errorcodes
let file = fs.openSync(filePath);
fs.fsync(file.fd).then(() => {
console.info("Data flushed");
fs.closeSync(file);
}).catch((err) => {
console.info("sync data failed with error message: " + err.message + ", error code: " + err.code);
});
......@@ -1851,7 +1888,7 @@ Lists all files in a folder. This API uses a promise to return the result.<br>Th
| Name | Type | Mandatory | Description |
| ------ | ------ | ---- | --------------------------- |
| recursion | boolean | No | Whether to list all files in subfolders recursively. The default value is **false**.|
| recursion | boolean | No | Whether to list all files in subfolders recursively. The default value is **false**. If **recursion** is **false**, the names of the files and folders that meet the specified conditions in the current directory are returned. If **recursion** is **true**, the relative paths (starting with /) of all files that meet the specified conditions in the directory are returned.|
| listNum | number | No | Number of file names to list. The default value **0** means to list all files.|
| filter | [Filter](#filter) | No | File filtering options. Currently, only the match by file name extension, fuzzy search by file name, and filter by file size or latest modification time are supported.|
......@@ -1873,7 +1910,7 @@ For details about the error codes, see [Basic File IO Error Codes](../errorcodes
"listNum": 0,
"filter": {
"suffix": [".png", ".jpg", ".jpeg"],
"displayName": ["%abc", "efg%"],
"displayName": ["*abc", "efg*"],
"fileSizeOver": 1024,
"lastModifiedAfter": new Date().getTime(),
}
......@@ -1911,7 +1948,7 @@ Lists all files in a folder. This API uses an asynchronous callback to return th
| Name | Type | Mandatory | Description |
| ------ | ------ | ---- | --------------------------- |
| recursion | boolean | No | Whether to list all files in subfolders recursively. The default value is **false**.|
| recursion | boolean | No | Whether to list all files in subfolders recursively. The default value is **false**. If **recursion** is **false**, the names of the files and folders that meet the specified conditions in the current directory are returned. If **recursion** is **true**, the relative paths (starting with /) of all files that meet the specified conditions in the directory are returned.|
| listNum | number | No | Number of file names to list. The default value **0** means to list all files.|
| filter | [Filter](#filter) | No | File filtering options. Currently, only the match by file name extension, fuzzy search by file name, and filter by file size or latest modification time are supported.|
......@@ -1927,7 +1964,7 @@ For details about the error codes, see [Basic File IO Error Codes](../errorcodes
"listNum": 0,
"filter": {
"suffix": [".png", ".jpg", ".jpeg"],
"displayName": ["%abc", "efg%"],
"displayName": ["*abc", "efg*"],
"fileSizeOver": 1024,
"lastModifiedAfter": new Date().getTime(),
}
......@@ -1967,7 +2004,7 @@ Lists all files in a folder synchronously. This API supports recursive listing o
| Name | Type | Mandatory | Description |
| ------ | ------ | ---- | --------------------------- |
| recursion | boolean | No | Whether to list all files in subfolders recursively. The default value is **false**.|
| recursion | boolean | No | Whether to list all files in subfolders recursively. The default value is **false**. If **recursion** is **false**, the names of the files and folders that meet the specified conditions in the current directory are returned. If **recursion** is **true**, the relative paths (starting with /) of all files that meet the specified conditions in the directory are returned.|
| listNum | number | No | Number of file names to list. The default value **0** means to list all files.|
| filter | [Filter](#filter) | No | File filtering options. Currently, only the match by file name extension, fuzzy search by file name, and filter by file size or latest modification time are supported.|
......@@ -1989,7 +2026,7 @@ For details about the error codes, see [Basic File IO Error Codes](../errorcodes
"listNum": 0,
"filter": {
"suffix": [".png", ".jpg", ".jpeg"],
"displayName": ["%abc", "efg%"],
"displayName": ["*abc", "efg*"],
"fileSizeOver": 1024,
"lastModifiedAfter": new Date().getTime(),
}
......@@ -2015,7 +2052,7 @@ Moves a directory. This API uses a promise to return the result.
| ------ | ------ | ---- | --------------------------- |
| src | string | Yes | Application sandbox path of the directory to move.|
| dest | string | Yes | Application sandbox path of the destination directory.|
| mode | number | No | Mode for moving the directory. The default value is **0**.<br>- **0**: Throw an exception if a directory conflict occurs.<br> Throw an exception if there is a directory with the same name in the destination directory.<br>- **1**: Throw an exception if a file conflict occurs.<br> Throw an exception if there is a directory with the same name in the destination directory and files with the same name exist in the conflicting directory. All the non-conflicting files in the source directory will be moved to the destination directory, and the non-conflicting files in the destination directory will be retained. The **data** attribute in the error returned provides information about the conflicting files in the Array\<[ConflictFiles](#conflictfiles)> format.<br>- **2**: Forcibly overwrite the conflicting files in the destination directory.<br>If there is a directory with the same name in the destination directory and files with the same name exist in the conflicting directory, all the files with the same name in the destination directory will be overwritten and the non-conflicting files will be retained.<br>- **3**: Forcibly overwrite the conflicting directory.<br> Move the source directory to the destination directory and overwrite the conflicting directory completely. That is, if there is a directory with the same name in the destination directory, all the original files in that directory will not be retained.|
| mode | number | No | Mode for moving the directory. The default value is **0**.<br>- **0**: Throw an exception if a directory conflict occurs.<br>Throw an exception if there is a non-empty directory with the same name in the destination directory.<br>- **1**: Throw an exception if a file conflict occurs.<br>Throw an exception if there is a directory with the same name in the destination directory and files with the same name exist in the conflicting directory. All the non-conflicting files in the source directory will be moved to the destination directory, and the non-conflicting files in the destination directory will be retained. The **data** attribute in the error returned provides information about the conflicting files in the Array\<[ConflictFiles](#conflictfiles10)> format.<br>- **2**: Forcibly overwrite the conflicting files in the destination directory. If there is a directory with the same name in the destination directory and files with the same name exist in the conflicting directory, all the files with the same name in the destination directory will be overwritten and the non-conflicting files will be retained.<br>- **3**: Forcibly overwrite the conflicting directory.<br>Move the source directory to the destination directory and overwrite the conflicting directory completely. That is, if there is a directory with the same name in the destination directory, all the original files in that directory will not be retained.|
**Return value**
......@@ -2049,7 +2086,7 @@ For details about the error codes, see [Basic File IO Error Codes](../errorcodes
## fs.moveDir<sup>10+</sup>
moveDir(src: string, dest: string, mode?: number, callback: AsyncCallback\<void>): void
moveDir(src: string, dest: string, mode?: number, callback: AsyncCallback\<void, Array\<ConflictFiles>>): void
Moves a directory. This API uses an asynchronous callback to return the result.
......@@ -2061,8 +2098,8 @@ Moves a directory. This API uses an asynchronous callback to return the result.
| ------ | ------ | ---- | --------------------------- |
| src | string | Yes | Application sandbox path of the source directory.|
| dest | string | Yes | Application sandbox path of the destination directory.|
| mode | number | No | Mode for moving the directory. The default value is **0**.<br>- **0**: Throw an exception if a directory conflict occurs.<br> Throw an exception if there is a directory with the same name in the destination directory.<br>- **1**: Throw an exception if a file conflict occurs.<br> Throw an exception if there is a directory with the same name in the destination directory and files with the same name exist in the conflicting directory. All the non-conflicting files in the source directory will be moved to the destination directory, and the non-conflicting files in the destination directory will be retained. The **data** attribute in the error returned provides information about the conflicting files in the Array\<[ConflictFiles](#conflictfiles)> format.<br>- **2**: Forcibly overwrite the conflicting files in the destination directory. If there is a directory with the same name in the destination directory and files with the same name exist in the conflicting directory, all the files with the same name in the destination directory will be overwritten and the non-conflicting files will be retained.<br>- **3**: Forcibly overwrite the conflicting directory.<br> Move the source directory to the destination directory and overwrite the conflicting directory completely. That is, if there is a directory with the same name in the destination directory, all the original files in that directory will not be retained.|
| callback | AsyncCallback&lt;void&gt; | Yes | Callback invoked when the directory is moved. |
| mode | number | No | Mode for moving the directory. The default value is **0**.<br>- **0**: Throw an exception if a directory conflict occurs.<br>Throw an exception if there is a directory with the same name in the destination directory.<br>- **1**: Throw an exception if a file conflict occurs.<br>Throw an exception if there is a directory with the same name in the destination directory and files with the same name exist in the conflicting directory. All the non-conflicting files in the source directory will be moved to the destination directory, and the non-conflicting files in the destination directory will be retained. The **data** attribute in the error returned provides information about the conflicting files in the Array\<[ConflictFiles](#conflictfiles10)> format.<br>- **2**: Forcibly overwrite the conflicting files in the destination directory. If there is a directory with the same name in the destination directory and files with the same name exist in the conflicting directory, all the files with the same name in the destination directory will be overwritten and the non-conflicting files will be retained.<br>- **3**: Forcibly overwrite the conflicting directory.<br>Move the source directory to the destination directory and overwrite the conflicting directory completely. That is, if there is a directory with the same name in the destination directory, all the original files in that directory will not be retained.|
| callback | AsyncCallback&lt;void, Array&lt;[ConflictFiles](#conflictfiles10)&gt;&gt; | Yes | Callback invoked when the directory is moved. |
**Error codes**
......@@ -2288,7 +2325,7 @@ For details about the error codes, see [Basic File IO Error Codes](../errorcodes
## fs.createRandomAccessFile<sup>10+</sup>
createRandomAccessFile(file: string|File, mode?: string): Promise&lt;RandomAccessFile&gt;
createRandomAccessFile(file: string|File, mode?: number): Promise&lt;RandomAccessFile&gt;
Creates a **RandomAccessFile** instance based on the specified file path or file object. This API uses a promise to return the result.
......@@ -2317,6 +2354,8 @@ For details about the error codes, see [Basic File IO Error Codes](../errorcodes
let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
fs.createRandomAccessFile(file).then((randomAccessFile) => {
console.info("randomAccessFile fd: " + randomAccessFile.fd);
randomAccessFile.close();
fs.closeSync(file);
}).catch((err) => {
console.info("create randomAccessFile failed with error message: " + err.message + ", error code: " + err.code);
});
......@@ -2325,7 +2364,7 @@ For details about the error codes, see [Basic File IO Error Codes](../errorcodes
## fs.createRandomAccessFile<sup>10+</sup>
createRandomAccessFile(file: string|File, mode?: string, callback: AsyncCallback&lt;RandomAccessFile&gt;): void
createRandomAccessFile(file: string|File, mode?: number, callback: AsyncCallback&lt;RandomAccessFile&gt;): void
Creates a **RandomAccessFile** instance based on the specified file path or file object. This API uses an asynchronous callback to return the result.
......@@ -2352,6 +2391,8 @@ For details about the error codes, see [Basic File IO Error Codes](../errorcodes
console.info("create randomAccessFile failed with error message: " + err.message + ", error code: " + err.code);
} else {
console.info("randomAccessFilefile fd: " + randomAccessFile.fd);
randomAccessFile.close();
fs.closeSync(file);
}
});
```
......@@ -2359,7 +2400,7 @@ For details about the error codes, see [Basic File IO Error Codes](../errorcodes
## fs.createRandomAccessFileSync<sup>10+</sup>
createRandomAccessFileSync(file: string|File, , mode?: string): RandomAccessFile
createRandomAccessFileSync(file: string|File, mode?: number): RandomAccessFile
Creates a **RandomAccessFile** instance based on the specified file path or file object.
......@@ -2386,8 +2427,10 @@ For details about the error codes, see [Basic File IO Error Codes](../errorcodes
```js
let filePath = pathDir + "/test.txt";
let file = fs.openSync(filePath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE);
let randomaccessfile = fileIO.createRandomAccessFileSync(file);
let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
let randomaccessfile = fs.createRandomAccessFileSync(file);
randomaccessfile.close();
fs.closeSync(file);
```
## fs.createStream
......@@ -3232,7 +3275,7 @@ Synchronously reads data from the stream.
| Name | Type | Mandatory | Description |
| ------- | ----------- | ---- | ---------------------------------------- |
| buffer | ArrayBuffer | Yes | Buffer used to store the file read. |
| options | Object | No | The options are as follows:<br>- **length** (number): length of the data to read. This parameter is optional. The default value is the buffer length.<br>- **offset** (number): start position to read the data. This parameter is optional. By default, data is read from the current position.<br> |
| options | Object | No | The options are as follows:<br>- **length** (number): length of the data to read. This parameter is optional. The default value is the buffer length.<br>- **offset** (number): start position to read the data. This parameter is optional. By default, data is read from the current position.<br>|
**Return value**
......@@ -3263,6 +3306,8 @@ Represents a **File** object opened by **open()**.
| Name | Type | Readable | Writable | Description |
| ---- | ------ | ---- | ---- | ------- |
| fd | number | Yes | No | FD of the file.|
| path<sup>10+</sup> | string | Yes | No | Path of the file.|
| name<sup>10+</sup> | string | Yes | No | Name of the file.|
### lock
......@@ -3410,6 +3455,7 @@ For details about the error codes, see [Basic File IO Error Codes](../errorcodes
let filePath = pathDir + "/test.txt";
let randomAccessFile = fs.createRandomAccessFileSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
randomAccessFile.setFilePointer(1);
randomAccessFile.close();
```
......@@ -3430,7 +3476,7 @@ For details about the error codes, see [Basic File IO Error Codes](../errorcodes
```js
let filePath = pathDir + "/test.txt";
let randomAccessFile = fs.createRandomAccessFileSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
randomAccessFile.closeSync();
randomAccessFile.close();
```
### write<sup>10+</sup>
......@@ -3462,11 +3508,13 @@ For details about the error codes, see [Basic File IO Error Codes](../errorcodes
```js
let filePath = pathDir + "/test.txt";
let file = fs.openSync(fpath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
let randomaccessfile = fs.createRandomAccessFileSync(file);
let bufferLength = 4096;
randomaccessfile.write(new ArrayBuffer(bufferLength), { offset: 1, length: 5 }).then((bytesWritten) => {
console.info("randomAccessFile bytesWritten: " + bytesWritten);
randomaccessfile.close();
fs.closeSync(file);
}).catch((err) => {
console.info("create randomAccessFile failed with error message: " + err.message + ", error code: " + err.code);
});
......@@ -3497,6 +3545,7 @@ For details about the error codes, see [Basic File IO Error Codes](../errorcodes
```js
let filePath = pathDir + "/test.txt";
let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
let randomAccessFile = fs.createRandomAccessFileSync(file);
let bufferLength = 4096;
randomAccessFile.write(new ArrayBuffer(bufferLength), { offset: 1 }, function(err, bytesWritten) {
......@@ -3505,6 +3554,8 @@ For details about the error codes, see [Basic File IO Error Codes](../errorcodes
} else {
if (bytesWritten) {
console.info("write succeed and size is:" + bytesWritten);
randomAccessFile.close();
fs.closeSync(file);
}
}
});
......@@ -3539,8 +3590,10 @@ For details about the error codes, see [Basic File IO Error Codes](../errorcodes
```js
let filePath = pathDir + "/test.txt";
let randomaccessfile= fs.createRandomAccessFileSync(filePath,"r+");
let randomaccessfile = fs.createRandomAccessFileSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
let bytesWritten = randomaccessfile.writeSync("hello, world", {offset: 5, length: 5, encoding :'utf-8'});
randomaccessfile.close();
fs.closeSync(file);
```
### read<sup>10+</sup>
......@@ -3577,6 +3630,8 @@ For details about the error codes, see [Basic File IO Error Codes](../errorcodes
let bufferLength = 4096;
randomaccessfile.read(new ArrayBuffer(bufferLength), { offset: 1, length: 5 }).then((readLength) => {
console.info("randomAccessFile readLength: " + readLength);
randomaccessfile.close();
fs.closeSync(file);
}).catch((err) => {
console.info("create randomAccessFile failed with error message: " + err.message + ", error code: " + err.code);
});
......@@ -3607,7 +3662,7 @@ For details about the error codes, see [Basic File IO Error Codes](../errorcodes
```js
let filePath = pathDir + "/test.txt";
let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
let randomaccessfile = await fileIO.createRandomAccessFile(file);
let randomaccessfile = fs.createRandomAccessFileSync(file);
let length = 20;
randomaccessfile.read(new ArrayBuffer(length), { offset: 1, length: 5 }, function (err, readLength) {
if (err) {
......@@ -3615,6 +3670,8 @@ For details about the error codes, see [Basic File IO Error Codes](../errorcodes
} else {
if (readLength) {
console.info("read succeed and size is:" + readLength);
randomaccessfile.close();
fs.closeSync(file);
}
}
});
......@@ -3633,7 +3690,7 @@ Synchronously reads data from a file.
| Name | Type | Mandatory | Description |
| ------- | ----------- | ---- | ---------------------------------------- |
| buffer | ArrayBuffer | Yes | Buffer used to store the file read. |
| options | Object | No | The options are as follows:<br>- **length** (number): length of the data to read. This parameter is optional. The default value is the buffer length.<br>- **offset** (number): start position to read the data (it is determined by **filePointer** plus **offset**). This parameter is optional. By default, data is read from the **filePointer**.<br> |
| options | Object | No | The options are as follows:<br>- **length** (number): length of the data to read. This parameter is optional. The default value is the buffer length.<br>- **offset** (number): start position to read the data (it is determined by **filePointer** plus **offset**). This parameter is optional. By default, data is read from the **filePointer**.<br>|
**Return value**
......@@ -3649,10 +3706,12 @@ For details about the error codes, see [Basic File IO Error Codes](../errorcodes
```js
let filePath = pathDir + "/test.txt";
let file = fs.openSync(filePath, fileIO.OpenMode.CREATE | fileIO.OpenMode.READ_WRITE);
let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
let randomaccessfile = fs.createRandomAccessFileSync(file);
let length = 4096;
let readLength = randomaccessfile.readSync(new ArrayBuffer(length));
randomaccessfile.close();
fs.closeSync(file);
```
......
......@@ -8,8 +8,13 @@
## Modules to Import
```js
> **NOTE**
>
> You need to import the **BusinessError** module if **BusinessError** is used in the sample code.
```ts
import picker from '@ohos.file.picker';
import { BusinessError } from '@ohos.base';
```
## PhotoViewPicker
......@@ -47,19 +52,20 @@ Selects one or more images or videos in a **photoPicker** page. This API uses a
**Example**
```ts
async function example() {
async function example01() {
try {
let PhotoSelectOptions = new picker.PhotoSelectOptions();
PhotoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE;
PhotoSelectOptions.maxSelectNumber = 5;
let photoPicker = new picker.PhotoViewPicker();
photoPicker.select(PhotoSelectOptions).then((PhotoSelectResult) => {
photoPicker.select(PhotoSelectOptions).then((PhotoSelectResult: picker.PhotoSelectResult) => {
console.info('PhotoViewPicker.select successfully, PhotoSelectResult uri: ' + JSON.stringify(PhotoSelectResult));
}).catch((err) => {
console.error('PhotoViewPicker.select failed with err: ' + err);
}).catch((err: BusinessError) => {
console.error('PhotoViewPicker.select failed with err: ' + JSON.stringify(err));
});
} catch (err) {
console.error('PhotoViewPicker failed with err: ' + err);
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error('PhotoViewPicker failed with err: ' + JSON.stringify(err));
}
}
```
......@@ -82,21 +88,22 @@ Selects one or more images or videos in a **photoPicker** page. This API uses an
**Example**
```ts
async function example() {
try {
async function example02() {
try {
let PhotoSelectOptions = new picker.PhotoSelectOptions();
PhotoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE;
PhotoSelectOptions.maxSelectNumber = 5;
let photoPicker = new picker.PhotoViewPicker();
photoPicker.select(PhotoSelectOptions, (err, PhotoSelectResult) => {
photoPicker.select(PhotoSelectOptions, (err: BusinessError, PhotoSelectResult: picker.PhotoSelectResult) => {
if (err) {
console.error('PhotoViewPicker.select failed with err: ' + err);
console.error('PhotoViewPicker.select failed with err: ' + JSON.stringify(err));
return;
}
console.info('PhotoViewPicker.select successfully, PhotoSelectResult uri: ' + JSON.stringify(PhotoSelectResult));
});
} catch (err) {
console.error('PhotoViewPicker failed with err: ' + err);
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error('PhotoViewPicker failed with err: ' + JSON.stringify(err));
}
}
```
......@@ -118,18 +125,19 @@ Selects one or more images or videos in a **photoPicker** page. This API uses an
**Example**
```ts
async function example() {
try {
async function example03() {
try {
let photoPicker = new picker.PhotoViewPicker();
photoPicker.select((err, PhotoSelectResult) => {
photoPicker.select((err: BusinessError, PhotoSelectResult: picker.PhotoSelectResult) => {
if (err) {
console.error('PhotoViewPicker.select failed with err: ' + err);
console.error('PhotoViewPicker.select failed with err: ' + JSON.stringify(err));
return;
}
console.info('PhotoViewPicker.select successfully, PhotoSelectResult uri: ' + JSON.stringify(PhotoSelectResult));
});
} catch (err) {
console.error('PhotoViewPicker failed with err: ' + err);
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error('PhotoViewPicker failed with err: ' + JSON.stringify(err));
}
}
```
......@@ -157,18 +165,19 @@ Saves one or more images or videos in a **photoPicker** page. This API uses a pr
**Example**
```ts
async function example() {
try {
async function example04() {
try {
let PhotoSaveOptions = new picker.PhotoSaveOptions();
PhotoSaveOptions.newFileNames = ['PhotoViewPicker01.jpg', 'PhotoViewPicker01.mp4'];
let photoPicker = new picker.PhotoViewPicker();
photoPicker.save(PhotoSaveOptions).then((PhotoSaveResult) => {
photoPicker.save(PhotoSaveOptions).then((PhotoSaveResult: Array<string>) => {
console.info('PhotoViewPicker.save successfully, PhotoSaveResult uri: ' + JSON.stringify(PhotoSaveResult));
}).catch((err) => {
console.error('PhotoViewPicker.save failed with err: ' + err);
}).catch((err: BusinessError) => {
console.error('PhotoViewPicker.save failed with err: ' + JSON.stringify(err));
});
} catch (err) {
console.error('PhotoViewPicker failed with err: ' + err);
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error('PhotoViewPicker failed with err: ' + JSON.stringify(err));
}
}
```
......@@ -191,20 +200,21 @@ Saves one or more images or videos in a **photoPicker** page. This API uses an a
**Example**
```ts
async function example() {
async function example05() {
try {
let PhotoSaveOptions = new picker.PhotoSaveOptions();
PhotoSaveOptions.newFileNames = ['PhotoViewPicker02.jpg','PhotoViewPicker02.mp4'];
let photoPicker = new picker.PhotoViewPicker();
photoPicker.save(PhotoSaveOptions, (err, PhotoSaveResult) => {
photoPicker.save(PhotoSaveOptions, (err: BusinessError, PhotoSaveResult: Array<string>) => {
if (err) {
console.error('PhotoViewPicker.save failed with err: ' + err);
console.error('PhotoViewPicker.save failed with err: ' + JSON.stringify(err));
return;
}
console.info('PhotoViewPicker.save successfully, PhotoSaveResult uri: ' + JSON.stringify(PhotoSaveResult));
});
} catch (err) {
console.error('PhotoViewPicker failed with err: ' + err);
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error('PhotoViewPicker failed with err: ' + JSON.stringify(err));
}
}
```
......@@ -226,25 +236,26 @@ Saves one or more images or videos in a **photoPicker** page. This API uses an a
**Example**
```ts
async function example() {
async function example06() {
try {
let photoPicker = new picker.PhotoViewPicker();
photoPicker.save((err, PhotoSaveResult) => {
photoPicker.save((err: BusinessError, PhotoSaveResult: Array<string>) => {
if (err) {
console.error('PhotoViewPicker.save failed with err: ' + err);
console.error('PhotoViewPicker.save failed with err: ' + JSON.stringify(err));
return;
}
console.info('PhotoViewPicker.save successfully, PhotoSaveResult uri: ' + JSON.stringify(PhotoSaveResult));
});
} catch (err) {
console.error('PhotoViewPicker failed with err: ' + err);
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error('PhotoViewPicker failed with err: ' + JSON.stringify(err));
}
}
```
## DocumentViewPicker
Provides APIs for selecting and saving non-media files, for example, documents in a variety of formats. Before using the APIs of **DocumentViewPicker**, you need to create a **DocumentViewPicker** instance.
Provides the **DocumentViewPicker** object for selecting and saving documents in different formats. Before using the APIs of **DocumentViewPicker**, you need to create a **DocumentViewPicker** instance.
**System capability**: SystemCapability.FileManagement.UserFileService
......@@ -277,17 +288,18 @@ Selects one or more documents in a **documentPicker** page. This API uses a prom
**Example**
```ts
async function example() {
async function example07() {
try {
let DocumentSelectOptions = new picker.DocumentSelectOptions();
let documentPicker = new picker.DocumentViewPicker();
documentPicker.select(DocumentSelectOptions).then((DocumentSelectResult) => {
documentPicker.select(DocumentSelectOptions).then((DocumentSelectResult: Array<string>) => {
console.info('DocumentViewPicker.select successfully, DocumentSelectResult uri: ' + JSON.stringify(DocumentSelectResult));
}).catch((err) => {
console.error('DocumentViewPicker.select failed with err: ' + err);
}).catch((err: BusinessError) => {
console.error('DocumentViewPicker.select failed with err: ' + JSON.stringify(err));
});
} catch (err) {
console.error('DocumentViewPicker failed with err: ' + err);
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error('DocumentViewPicker failed with err: ' + JSON.stringify(err));
}
}
```
......@@ -310,19 +322,20 @@ Selects one or more documents in a **documentPicker** page. This API uses an asy
**Example**
```ts
async function example() {
async function example08() {
try {
let DocumentSelectOptions = new picker.DocumentSelectOptions();
let documentPicker = new picker.DocumentViewPicker();
documentPicker.select(DocumentSelectOptions, (err, DocumentSelectResult) => {
documentPicker.select(DocumentSelectOptions, (err: BusinessError, DocumentSelectResult: Array<string>) => {
if (err) {
console.error('DocumentViewPicker.select failed with err: ' + err);
console.error('DocumentViewPicker.select failed with err: ' + JSON.stringify(err));
return;
}
console.info('DocumentViewPicker.select successfully, DocumentSelectResult uri: ' + JSON.stringify(DocumentSelectResult));
});
} catch (err) {
console.error('DocumentViewPicker failed with err: ' + err);
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error('DocumentViewPicker failed with err: ' + JSON.stringify(err));
}
}
```
......@@ -344,18 +357,19 @@ Selects one or more documents in a **documentPicker** page. This API uses an asy
**Example**
```ts
async function example() {
async function example09() {
try {
let documentPicker = new picker.DocumentViewPicker();
documentPicker.select((err, DocumentSelectResult) => {
documentPicker.select((err: BusinessError, DocumentSelectResult: Array<string>) => {
if (err) {
console.error('DocumentViewPicker.select failed with err: ' + err);
console.error('DocumentViewPicker.select failed with err: ' + JSON.stringify(err));
return;
}
console.info('DocumentViewPicker.select successfully, DocumentSelectResult uri: ' + JSON.stringify(DocumentSelectResult));
});
} catch (err) {
console.error('DocumentViewPicker failed with err: ' + err);
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error('DocumentViewPicker failed with err: ' + JSON.stringify(err));
}
}
```
......@@ -384,18 +398,19 @@ Saves one or more documents in a **documentPicker** page. This API uses a promis
**Example**
```ts
async function example() {
async function example10() {
try {
let DocumentSaveOptions = new picker.DocumentSaveOptions();
DocumentSaveOptions.newFileNames = ['DocumentViewPicker01.txt'];
let documentPicker = new picker.DocumentViewPicker();
documentPicker.save(DocumentSaveOptions).then((DocumentSaveResult) => {
documentPicker.save(DocumentSaveOptions).then((DocumentSaveResult: Array<string>) => {
console.info('DocumentViewPicker.save successfully, DocumentSaveResult uri: ' + JSON.stringify(DocumentSaveResult));
}).catch((err) => {
console.error('DocumentViewPicker.save failed with err: ' + err);
}).catch((err: BusinessError) => {
console.error('DocumentViewPicker.save failed with err: ' + JSON.stringify(err));
});
} catch (err) {
console.error('DocumentViewPicker failed with err: ' + err);
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error('DocumentViewPicker failed with err: ' + JSON.stringify(err));
}
}
```
......@@ -418,20 +433,21 @@ Saves one or more documents in a **documentPicker** page. This API uses an async
**Example**
```ts
async function example() {
async function example11() {
try {
let DocumentSaveOptions = new picker.DocumentSaveOptions();
DocumentSaveOptions.newFileNames = ['DocumentViewPicker02.txt'];
let documentPicker = new picker.DocumentViewPicker();
documentPicker.save(DocumentSaveOptions, (err, DocumentSaveResult) => {
documentPicker.save(DocumentSaveOptions, (err: BusinessError, DocumentSaveResult: Array<string>) => {
if (err) {
console.error('DocumentViewPicker.save failed with err: ' + err);
console.error('DocumentViewPicker.save failed with err: ' + JSON.stringify(err));
return;
}
console.info('DocumentViewPicker.save successfully, DocumentSaveResult uri: ' + JSON.stringify(DocumentSaveResult));
});
} catch (err) {
console.error('DocumentViewPicker failed with err: ' + err);
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error('DocumentViewPicker failed with err: ' + JSON.stringify(err));
}
}
```
......@@ -453,18 +469,19 @@ Saves one or more documents in a **documentPicker** page. This API uses an async
**Example**
```ts
async function example() {
async function example12() {
try {
let documentPicker = new picker.DocumentViewPicker();
documentPicker.save((err, DocumentSaveResult) => {
documentPicker.save((err: BusinessError, DocumentSaveResult: Array<string>) => {
if (err) {
console.error('DocumentViewPicker.save failed with err: ' + err);
console.error('DocumentViewPicker.save failed with err: ' + JSON.stringify(err));
return;
}
console.info('DocumentViewPicker.save successfully, DocumentSaveResult uri: ' + JSON.stringify(DocumentSaveResult));
});
} catch (err) {
console.error('DocumentViewPicker failed with err: ' + err);
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error('DocumentViewPicker failed with err: ' + JSON.stringify(err));
}
}
```
......@@ -504,17 +521,18 @@ Selects one or more audio files in an **audioPicker** page (currently, a **docum
**Example**
```ts
async function example() {
async function example13() {
try {
let AudioSelectOptions = new picker.AudioSelectOptions();
let audioPicker = new picker.AudioViewPicker();
audioPicker.select(AudioSelectOptions).then((AudioSelectResult) => {
audioPicker.select(AudioSelectOptions).then((AudioSelectResult: Array<string>) => {
console.info('AudioViewPicker.select successfully, AudioSelectResult uri: ' + JSON.stringify(AudioSelectResult));
}).catch((err) => {
console.error('AudioViewPicker.select failed with err: ' + err);
}).catch((err: BusinessError) => {
console.error('AudioViewPicker.select failed with err: ' + JSON.stringify(err));
});
} catch (err) {
console.error('AudioViewPicker failed with err: ' + err);
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error('AudioViewPicker failed with err: ' + JSON.stringify(err));
}
}
```
......@@ -537,19 +555,20 @@ Selects one or more audio files in an **audioPicker** page (currently, a **docum
**Example**
```ts
async function example() {
async function example14() {
try {
let AudioSelectOptions = new picker.AudioSelectOptions();
let audioPicker = new picker.AudioViewPicker();
audioPicker.select(AudioSelectOptions, (err, AudioSelectResult) => {
audioPicker.select(AudioSelectOptions, (err: BusinessError, AudioSelectResult: Array<string>) => {
if (err) {
console.error('AudioViewPicker.select failed with err: ' + err);
console.error('AudioViewPicker.select failed with err: ' + JSON.stringify(err));
return;
}
console.info('AudioViewPicker.select successfully, AudioSelectResult uri: ' + JSON.stringify(AudioSelectResult));
});
} catch (err) {
console.error('AudioViewPicker failed with err: ' + err);
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error('AudioViewPicker failed with err: ' + JSON.stringify(err));
}
}
```
......@@ -571,18 +590,19 @@ Selects one or more audio files in an **audioPicker** page (currently, a **docum
**Example**
```ts
async function example() {
async function example15() {
try {
let audioPicker = new picker.AudioViewPicker();
audioPicker.select((err, AudioSelectResult) => {
audioPicker.select((err: BusinessError, AudioSelectResult: Array<string>) => {
if (err) {
console.error('AudioViewPicker.select failed with err: ' + err);
console.error('AudioViewPicker.select failed with err: ' + JSON.stringify(err));
return;
}
console.info('AudioViewPicker.select successfully, AudioSelectResult uri: ' + JSON.stringify(AudioSelectResult));
});
} catch (err) {
console.error('AudioViewPicker failed with err: ' + err);
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error('AudioViewPicker failed with err: ' + JSON.stringify(err));
}
}
```
......@@ -610,18 +630,19 @@ Saves one or more audio files in an **audioPicker** page (currently, a **documen
**Example**
```ts
async function example() {
async function example16() {
try {
let AudioSaveOptions = new picker.AudioSaveOptions();
AudioSaveOptions.newFileNames = ['AudioViewPicker01.mp3'];
let audioPicker = new picker.AudioViewPicker();
audioPicker.save(AudioSaveOptions).then((AudioSaveResult) => {
audioPicker.save(AudioSaveOptions).then((AudioSaveResult: Array<string>) => {
console.info('AudioViewPicker.save successfully, AudioSaveResult uri: ' + JSON.stringify(AudioSaveResult))
}).catch((err) => {
console.error('AudioViewPicker.save failed with err: ' + err);
}).catch((err: BusinessError) => {
console.error('AudioViewPicker.save failed with err: ' + JSON.stringify(err));
});
} catch (err) {
console.error('AudioViewPicker failed with err: ' + err);
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error('AudioViewPicker failed with err: ' + JSON.stringify(err));
}
}
```
......@@ -644,20 +665,21 @@ Saves one or more audio files in an **audioPicker** page (currently, a **documen
**Example**
```ts
async function example() {
async function example17() {
try {
let AudioSaveOptions = new picker.AudioSaveOptions();
AudioSaveOptions.newFileNames = ['AudioViewPicker02.mp3'];
let audioPicker = new picker.AudioViewPicker();
audioPicker.save(AudioSaveOptions, (err, AudioSaveResult) => {
audioPicker.save(AudioSaveOptions, (err: BusinessError, AudioSaveResult: Array<string>) => {
if (err) {
console.error('AudioViewPicker.save failed with err: ' + err);
console.error('AudioViewPicker.save failed with err: ' + JSON.stringify(err));
return;
}
console.info('AudioViewPicker.save successfully, AudioSaveResult uri: ' + JSON.stringify(AudioSaveResult));
});
} catch (err) {
console.error('AudioViewPicker failed with err: ' + err);
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error('AudioViewPicker failed with err: ' + JSON.stringify(err));
}
}
```
......@@ -679,18 +701,19 @@ Saves one or more audio files in an **audioPicker** page (currently, a **documen
**Example**
```ts
async function example() {
async function example18() {
try {
let audioPicker = new picker.AudioViewPicker();
audioPicker.save((err, AudioSaveResult) => {
audioPicker.save((err: BusinessError, AudioSaveResult: Array<string>) => {
if (err) {
console.error('AudioViewPicker.save failed with err: ' + err);
console.error('AudioViewPicker.save failed with err: ' + JSON.stringify(err));
return;
}
console.info('AudioViewPicker.save successfully, AudioSaveResult uri: ' + JSON.stringify(AudioSaveResult));
});
} catch (err) {
console.error('AudioViewPicker failed with err: ' + err);
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error('AudioViewPicker failed with err: ' + JSON.stringify(err));
}
}
```
......@@ -715,8 +738,8 @@ Defines the options for selecting images or videos.
| Name | Type | Mandatory| Description |
| ----------------------- | ------------------- | ---- | -------------------------------- |
| MIMEType? | [PhotoViewMIMETypes](#photoviewmimetypes) | No | Available media file types. **IMAGE_VIDEO_TYPE** is used by default.|
| maxSelectNumber? | number | No | Maximum number of media files to select. The default value is **50**, and the maximum value is **500**. |
| MIMEType | [PhotoViewMIMETypes](#photoviewmimetypes) | No | Available media file types. **IMAGE_VIDEO_TYPE** is used by default.|
| maxSelectNumber | number | No | Maximum number of media files to select. The default value is **50**, and the maximum value is **500**. |
## PhotoSelectResult
......@@ -737,14 +760,20 @@ Defines the options for saving images or videos.
| Name | Type | Mandatory| Description |
| ----------------------- | ------------------- | ---- | ---------------------------- |
| newFileNames? | Array&lt;string&gt; | No | Names of the files to save. If this parameter is not specified, the user needs to enter the file names.|
| newFileNames | Array&lt;string&gt; | No | Names of the files to save. If this parameter is not specified, the user needs to enter the file names.|
## DocumentSelectOptions
Defines the options for selecting documents. Currently, this parameter cannot be configured.
Defines the options for selecting documents.
**System capability**: SystemCapability.FileManagement.UserFileService
| Name | Type | Mandatory| Description |
| ----------------------- | ------------------- | ---- | -------------------------------- |
| maxSelectNumber<sup>10+</sup> | number | No | Maximum number of files or directories that can be selected.<br>Value range: 1–500<br>Maximum value: **500** |
| defaultFilePathUri<sup>10+</sup> | string | No | Path of the file or directory to select.|
| fileSuffixFilters<sup>10+</sup> | Array&lt;string&gt; | No | File name extensions of the documents to select.|
## DocumentSaveOptions
Defines the options for saving documents.
......@@ -753,7 +782,9 @@ Defines the options for saving documents.
| Name | Type | Mandatory| Description |
| ----------------------- | ------------------- | ---- | ---------------------------- |
| newFileNames? | Array&lt;string&gt; | No | Names of the documents to save. If this parameter is not specified, the user needs to enter the document names. |
| newFileNames | Array&lt;string&gt; | No | Names of the documents to save. If this parameter is not specified, the user needs to enter the document names. |
| defaultFilePathUri<sup>10+</sup> | string | No | Path of the file or directory to save.|
| fileSuffixChoices<sup>10+</sup> | Array&lt;string&gt; | No | File name extensions of the documents to save.|
## AudioSelectOptions
......@@ -769,4 +800,4 @@ Defines the options for saving audio files.
| Name | Type | Mandatory| Description |
| ----------------------- | ------------------- | ---- | ---------------------------- |
| newFileNames? | Array&lt;string&gt; | No | Name of the audio files to save. If this parameter is not specified, the user needs to enter the file names.|
| newFileNames | Array&lt;string&gt; | No | Name of the audio files to save. If this parameter is not specified, the user needs to enter the file names.|
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册