1# 文件管理子系统ChangeLog 2 3## cl.filemanagement.1 fileio相关接口异常处理方式变更 4 5file_api部件fileio接口返回值不包含错误码error.code,现进行错误码整改,废弃原有相关接口,新增相关接口。 6 7**变更影响** 8 9基于此前版本开发的应用,需注意废弃接口的迭代更新。新接口在接口规格上进行了微调,需注意新接口使用方法。 10 11**关键接口/组件变更** 12 13为适配统一的API异常处理方式,对fileio相关接口进行废弃,并新增对应接口,原接口位于@ohos.fileio,新接口位于@ohos.file.fs。新增接口支持统一的错误码异常处理规范,功能上与原接口保持一致,参数上有微调。 14 15| 模块名 | 方法/属性/枚举/常量 | 变更类型 | 16| ------------------------- | ------------------------------------------------------------ | -------- | 17| @ohos.fileio | **function** open(path: string, flags?: number, mode?: number, callback?: AsyncCallback<number>): void \| Promise<number>; | 废弃 | 18| @ohos.fileio | **function** openSync(path: string, flags?: number, mode?: number): number; | 废弃 | 19| @ohos.file.fs | **function** open(path: string, mode?: number, callback?: AsyncCallback<File>): void \| Promise<File>; | 新增 | 20| @ohos.file.fs | **function** openSync(path: string, mode?: number): File; | 新增 | 21| @ohos.fileio | **function** read(fd: number, buffer: ArrayBuffer, options?: { offset?: number; length?: number; position?: number; }, callback?: AsyncCallback<ReadOut>): void \| Promise<ReadOut>; | 废弃 | 22| @ohos.fileio | **function** readSync(fd: number, buffer: ArrayBuffer, options?: { offset?: number; length?: number; position?: number; }): number; | 废弃 | 23| @ohos.file.fs | **function** read(fd: number, buffer: ArrayBuffer, options?: { offset?: number; length?: number; }, callback?: AsyncCallback<number>): void \| Promise<number>; | 新增 | 24| @ohos.file.fs | **function** readSync(fd: number, buffer: ArrayBuffer, options?: { offset?: number; length?: number; }): number; | 新增 | 25| @ohos.fileio | **function** stat(path: string, callback?: AsyncCallback<Stat>): void \| Promise<Stat>; | 废弃 | 26| @ohos.fileio | **function** statSync(path: string): Stat; | 废弃 | 27| @ohos.fileio | **function** fstat(fd: number, callback?: AsyncCallback<Stat>): void \| Promise<Stat>; | 废弃 | 28| @ohos.fileio | **function** fstatSync(fd: number): Stat; | 废弃 | 29| @ohos.file.fs | **function** stat(file: string \| number, callback?: AsyncCallback<Stat>): void \| Promise<Stat>; | 新增 | 30| @ohos.file.fs | **function** statSync(file: string \| number): Stat; | 新增 | 31| @ohos.fileio | **function** truncate(path: string, len?: number, callback?: AsyncCallback<void>): void \| Promise<void>; | 废弃 | 32| @ohos.fileio | **function** truncateSync(path: string, len?: number): void; | 废弃 | 33| @ohos.fileio | **function** ftruncate(fd: number, len?: number, callback?: AsyncCallback<void>): void \| Promise<void>; | 废弃 | 34| @ohos.fileio | **function** ftruncateSync(fd: number, len?: number): void; | 废弃 | 35| @ohos.file.fs | **function** truncate(file: string \| number, len?: number, callback?: AsyncCallback<void>): void \| Promise<void>; | 新增 | 36| @ohos.file.fs | **function** truncateSync(file: string \| number, len?: number): void; | 新增 | 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>; | 废弃 | 38| @ohos.fileio | **function** writeSync(fd: number, buffer: ArrayBuffer \| string, options?: { offset?: number; length?: number; position?: number; encoding?: string; }): number; | 废弃 | 39| @ohos.file.fs | **function** write(fd: number, buffer: ArrayBuffer \| string, options?: { offset?: number; length?: number; encoding?: string; }, callback?: AsyncCallback<number>): void \| Promise<void>; | 新增 | 40| @ohos.file.fs | **function** writeSync(fd: number, buffer: ArrayBuffer \| string, options?: { offset?: number; length?: number; encoding?: string; }): number; | 新增 | 41 42**适配指导** 43 44原接口使用的是@ohos.fileio,以以下方式import: 45 46```js 47import fileio from '@ohos.fileio'; 48``` 49 50现新接口使用的是@ohos.file.fs,以以下方式import: 51 52```js 53import fs from '@ohos.file.fs'; 54``` 55 56此外还需要适配异常处理,同步接口异常处理示例代码: 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``` 66异步接口promise方法异常处理示例代码: 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 77异步接口callback方法异常处理示例代码: 78```js 79import fs from '@ohos.file.fs' 80 81try { 82 fs.open(path, fs.OpenMode.READ_ONLY, function(e, file){ //异步线程的错误(如系统调用等)在回调中获取 83 if (e) { 84 console.error("open in async errCode:" + e.code + ", errMessage:" + e.message); 85 } 86 }); 87} catch (err) { //主线程的错误(如非法参数等)通过try catch获取 88 console.error("open callback errCode:" + err.code + ", errMessage:" + err.message); 89} 90``` 91