为了屏蔽系统环境差异与小程序间数据隔离,特制定 fs:// 路径格式的文件系统。
import * as ymx from "@ymx/core";
import fileIo from "@ohos.file.fs";
const fsFilePath = 'fs://file/my_dir/my.txt';
let file: fileIo.File | undefined;
try {
//使用 apis.file.* 接口操作 fs 文件
await ymx.apis.file.write(fsFilePath, 'hello 世界 😃');
//将 fs 路径转换为 file:// URI
const fileUri = ymx.apis.file.toUri(fsFilePath);
//使用操作系统接口处理 file URI 路径
file = await fileIo.open(fileUri, fileIo.OpenMode.CREATE | fileIo.OpenMode.WRITE_ONLY);
await fileIo.write(file.fd, 'hello 世界 😃');
} catch (err) {
console.error(err);
} finally {
if (file) {
try {
await fileIo.close(file.fd);
} catch {
}
}
}