1# File Management Subsystem ChangeLog
2
3## cl.filemanagement.1 File I/O API Changes
4
5The return value of file I/O APIs of **file_api** does not contain the error code. The original APIs are deprecated, and new APIs are added.
6
7**Change Impacts**
8
9For applications developed based on earlier versions, pay attention to the iterative update of discarded APIs. The specifications of the new APIs are slightly adjusted. Pay attention to the usage of the new APIs.
10
11**Key API/Component Changes**
12
13For the adaptation to the unified API exception handling mode, related file I/O APIs are deprecated, and corresponding new APIs are added. Original APIs are stored in **@ohos.fileio**, and the new ones are stored in **@ohos.file.fs**. The newly added APIs support unified error code handling specifications and function the same as the original APIs. The parameters are slightly adjusted.
14
15| Module       | Method/Attribute/Enumeration/Constant                                         | Change Type|
16| ------------- | ------------------------------------------------------------ | -------- |
17| @ohos.fileio  | **function** open(path: string, flags?: number, mode?: number, callback?: AsyncCallback<number>): void \| Promise<number>; | Deprecated    |
18| @ohos.fileio  | **function** openSync(path: string, flags?: number, mode?: number): number; | Deprecated    |
19| @ohos.file.fs | **function**  open(path: string, mode?: number, callback?: AsyncCallback<File>): void \| Promise<File>; | Added    |
20| @ohos.file.fs | **function** openSync(path: string, mode?: number): File;    | Added    |
21| @ohos.fileio  | **function** read(fd: number, buffer: ArrayBuffer, options?: { offset?: number; length?: number; position?: number; }, callback?: AsyncCallback<ReadOut>): void \| Promise<ReadOut>; | Deprecated    |
22| @ohos.fileio  | **function** readSync(fd: number, buffer: ArrayBuffer, options?: { offset?: number; length?: number; position?: number; }): number; | Deprecated    |
23| @ohos.file.fs | **function** read(fd: number, buffer: ArrayBuffer, options?: { offset?: number; length?: number; }, callback?: AsyncCallback<number>): void \| Promise<number>; | Added    |
24| @ohos.file.fs | **function** readSync(fd: number, buffer: ArrayBuffer, options?: { offset?: number; length?: number; }): number; | Added    |
25| @ohos.fileio  | **function** stat(path: string, callback?: AsyncCallback<Stat>): void \| Promise<Stat>; | Deprecated    |
26| @ohos.fileio  | **function** statSync(path: string): Stat;                   | Deprecated    |
27| @ohos.fileio  | **function** fstat(fd: number, callback?: AsyncCallback<Stat>): void \| Promise<Stat>; | Deprecated    |
28| @ohos.fileio  | **function** fstatSync(fd: number): Stat;                    | Deprecated    |
29| @ohos.file.fs | **function** stat(file: string \| number, callback?: AsyncCallback<Stat>): void \| Promise<Stat>; | Added    |
30| @ohos.file.fs | **function** statSync(file: string \| number): Stat;         | Added    |
31| @ohos.fileio  | **function** truncate(path: string, len?: number, callback?: AsyncCallback<void>): void \| Promise<void>; | Deprecated    |
32| @ohos.fileio  | **function** truncateSync(path: string, len?: number): void; | Deprecated    |
33| @ohos.fileio  | **function** ftruncate(fd: number, len?: number, callback?: AsyncCallback<void>): void \| Promise<void>; | Deprecated    |
34| @ohos.fileio  | **function** ftruncateSync(fd: number, len?: number): void;  | Deprecated    |
35| @ohos.file.fs | **function** truncate(file: string \| number, len?: number, callback?: AsyncCallback<void>): void \| Promise<void>; | Added    |
36| @ohos.file.fs | **function** truncateSync(file: string \| number, len?: number): void; | Added    |
37| @ohos.fileio  | **function** write(fd: number, buffer: ArrayBuffer \| string, options?: { offset?: number; length?: number; position?: number; encoding?: string; }, callback?: AsyncCallback<number>): void \| Promise<void>; | Deprecated    |
38| @ohos.fileio  | **function** writeSync(fd: number, buffer: ArrayBuffer \| string, options?: { offset?: number; length?: number; position?: number; encoding?: string; }): number; | Deprecated    |
39| @ohos.file.fs | **function** write(fd: number, buffer: ArrayBuffer \| string, options?: { offset?: number; length?: number; encoding?: string; }, callback?: AsyncCallback<number>): void \| Promise<void>; | Added    |
40| @ohos.file.fs | **function** writeSync(fd: number, buffer: ArrayBuffer \| string, options?: { offset?: number; length?: number; encoding?: string; }): number; | Added    |
41
42**Adaptation Guide**
43
44The original APIs use **@ohos.fileio**, which is imported as follows:
45
46```js
47import fileio from '@ohos.fileio';
48```
49
50The new APIs use **@ohos.file.fs**, which is imported as follows:
51
52```js
53import fs from '@ohos.file.fs';
54```
55
56In addition, exception handling needs to be adapted. Sample code for synchronous API exception handling is as follows:
57```js
58import fs from '@ohos.file.fs'
59
60try {
61    let file = fs.openSync(path, fs.OpenMode.READ_ONLY);
62} catch (err) {
63    console.error("openSync errCode:" + err.code + ", errMessage:" + err.message);
64}
65```
66Sample code for handling exceptions of the **promise** method of an asynchronous API:
67```js
68import fs from '@ohos.file.fs'
69
70try {
71    let file = await fs.open(path, fs.OpenMode.READ_ONLY);
72} catch (err) {
73    console.error("open promise errCode:" + err.code + ", errMessage:" + err.message);
74}
75```
76
77Sample code for handling exceptions of the **callback** method of an asynchronous API:
78```js
79import fs from '@ohos.file.fs'
80
81try {
82    fs.open(path, fs.OpenMode.READ_ONLY, function(e, file){ // Asynchronous thread (such as system call) errors are obtained from the callback.
83        if (e) {
84            console.error("open in async errCode:" + e.code + ", errMessage:" + e.message);
85        }
86    });
87} catch (err) {// Errors (such as invalid parameters) of the main thread are obtained through try catch.
88    console.error("open callback errCode:" + err.code + ", errMessage:" + err.message);
89}
90```
91