openharmony 鸿蒙 changelogs-filemanagement

2023-02-03 浏览 (643)

File Management Subsystem ChangeLog

cl.filemanagement.1 File I/O API Changes

The 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.

Change Impacts

For 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.

Key API/Component Changes

For 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.

ModuleMethod/Attribute/Enumeration/ConstantChange Type
@ohos.fileiofunction open(path: string, flags?: number, mode?: number, callback?: AsyncCallback): void |Promise;Deprecated
@ohos.fileiofunction openSync(path: string, flags?: number, mode?: number): number;Deprecated
@ohos.file.fsfunction open(path: string, mode?: number, callback?: AsyncCallback): void |Promise;Added
@ohos.file.fsfunction openSync(path: string, mode?: number): File;Added
@ohos.fileiofunction read(fd: number, buffer: ArrayBuffer, options?: { offset?: number; length?: number; position?: number; }, callback?: AsyncCallback): void |Promise;Deprecated
@ohos.fileiofunction readSync(fd: number, buffer: ArrayBuffer, options?: { offset?: number; length?: number; position?: number; }): number;Deprecated
@ohos.file.fsfunction read(fd: number, buffer: ArrayBuffer, options?: { offset?: number; length?: number; }, callback?: AsyncCallback): void |Promise;Added
@ohos.file.fsfunction readSync(fd: number, buffer: ArrayBuffer, options?: { offset?: number; length?: number; }): number;Added
@ohos.fileiofunction stat(path: string, callback?: AsyncCallback): void |Promise;Deprecated
@ohos.fileiofunction statSync(path: string): Stat;Deprecated
@ohos.fileiofunction fstat(fd: number, callback?: AsyncCallback): void |Promise;Deprecated
@ohos.fileiofunction fstatSync(fd: number): Stat;Deprecated
@ohos.file.fsfunction stat(file: string |number, callback?: AsyncCallback): void |Promise;Added
@ohos.file.fsfunction statSync(file: string |number): Stat;Added
@ohos.fileiofunction truncate(path: string, len?: number, callback?: AsyncCallback): void |Promise;Deprecated
@ohos.fileiofunction truncateSync(path: string, len?: number): void;Deprecated
@ohos.fileiofunction ftruncate(fd: number, len?: number, callback?: AsyncCallback): void |Promise;Deprecated
@ohos.fileiofunction ftruncateSync(fd: number, len?: number): void;Deprecated
@ohos.file.fsfunction truncate(file: string |number, len?: number, callback?: AsyncCallback): void |Promise;Added
@ohos.file.fsfunction truncateSync(file: string |number, len?: number): void;Added
@ohos.fileiofunction write(fd: number, buffer: ArrayBuffer |string, options?: { offset?: number; length?: number; position?: number; encoding?: string; }, callback?: AsyncCallback): void |Promise;Deprecated
@ohos.fileiofunction writeSync(fd: number, buffer: ArrayBuffer |string, options?: { offset?: number; length?: number; position?: number; encoding?: string; }): number;Deprecated
@ohos.file.fsfunction write(fd: number, buffer: ArrayBuffer |string, options?: { offset?: number; length?: number; encoding?: string; }, callback?: AsyncCallback): void |Promise;Added
@ohos.file.fsfunction writeSync(fd: number, buffer: ArrayBuffer |string, options?: { offset?: number; length?: number; encoding?: string; }): number;Added

Adaptation Guide

The original APIs use @ohos.fileio, which is imported as follows:

import fileio from '@ohos.fileio';

The new APIs use @ohos.file.fs, which is imported as follows:

import fs from '@ohos.file.fs';

In addition, exception handling needs to be adapted. Sample code for synchronous API exception handling is as follows:

import fs from '@ohos.file.fs'

try {
    let file = fs.openSync(path, fs.OpenMode.READ_ONLY);
} catch (err) {
    console.error("openSync errCode:" + err.code + ", errMessage:" + err.message);
}

Sample code for handling exceptions of the promise method of an asynchronous API:

import fs from '@ohos.file.fs'

try {
    let file = await fs.open(path, fs.OpenMode.READ_ONLY);
} catch (err) {
    console.error("open promise errCode:" + err.code + ", errMessage:" + err.message);
}

Sample code for handling exceptions of the callback method of an asynchronous API:

import fs from '@ohos.file.fs'

try {
    fs.open(path, fs.OpenMode.READ_ONLY, function(e, file){ // Asynchronous thread (such as system call) errors are obtained from the callback.
        if (e) {
            console.error("open in async errCode:" + e.code + ", errMessage:" + e.message);
        }
    });
} catch (err) {// Errors (such as invalid parameters) of the main thread are obtained through try catch.
    console.error("open callback errCode:" + err.code + ", errMessage:" + err.message);
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Account Subsystem ChangeLog

harmony 鸿蒙ChangeLog of JS API Changes in the Multimedia Subsystem

harmony 鸿蒙Device Manager ChangeLog

harmony 鸿蒙USB Manager ChangeLog

harmony 鸿蒙DSoftBus Subsystem ChangeLog

harmony 鸿蒙Globalization Subsystem ChangeLog

harmony 鸿蒙Input Method Framework ChangeLog

harmony 鸿蒙Multimodal Input ChangeLog

harmony 鸿蒙Power Subsystem ChangeLog

harmony 鸿蒙Upload and Download Subsystem ChangeLog

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/60c3673c248c4bdbbfd1d1fcf8f99ebc