当前位置: 代码网 > it编程>游戏开发>ar > HarmonyOS实战开发:@ohos.file.fs (文件管理)

HarmonyOS实战开发:@ohos.file.fs (文件管理)

2024年08月03日 ar 我要评论
该模块为基础文件操作API,提供基础文件操作能力,包括文件基本管理、文件目录管理、文件信息统计、文件流式读写等常用功能。

该模块为基础文件操作api,提供基础文件操作能力,包括文件基本管理、文件目录管理、文件信息统计、文件流式读写等常用功能。

导入模块

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

使用说明

使用该功能模块对文件/目录进行操作前,需要先获取其应用沙箱路径,获取方式及其接口用法请参考:

stage模型

import uiability from '@ohos.app.ability.uiability';
import window from '@ohos.window';

export default class entryability extends uiability {
  onwindowstagecreate(windowstage: window.windowstage) {
    let context = this.context;
    let pathdir = context.filesdir;
  }
}

fa模型

import featureability from '@ohos.ability.featureability';

let context = featureability.getcontext();
context.getfilesdir().then((data) => {
  let pathdir = data;
})

fa模型context的具体获取方法参见fa模型

fs.stat

stat(file: string | number): promise<stat>

获取文件详细属性信息,使用promise异步返回。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
filestring | number文件应用沙箱路径path或已打开的文件描述符fd。

返回值:

类型说明
promise<stat>promise对象。返回文件的具体信息。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
fs.stat(filepath).then((stat: fs.stat) => {
  console.info("get file info succeed, the size of file is " + stat.size);
}).catch((err: businesserror) => {
  console.error("get file info failed with error message: " + err.message + ", error code: " + err.code);
});

fs.stat

stat(file: string | number, callback: asynccallback<stat>): void

获取文件详细属性信息,使用callback异步回调。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
filestring | number文件应用沙箱路径path或已打开的文件描述符fd。
callbackasynccallback<stat>异步获取文件的信息之后的回调。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
fs.stat(pathdir, (err: businesserror, stat: fs.stat) => {
  if (err) {
    console.error("get file info failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("get file info succeed, the size of file is " + stat.size);
  }
});copy to clipboarderrorcopied

fs.statsync

statsync(file: string | number): stat

以同步方法获取文件详细属性信息。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
filestring | number文件应用沙箱路径path或已打开的文件描述符fd。

返回值:

类型说明
stat表示文件的具体信息。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let stat = fs.statsync(pathdir);
console.info("get file info succeed, the size of file is " + stat.size);copy to clipboarderrorcopied

fs.access

access(path: string): promise<boolean>

检查文件是否存在,使用promise异步返回。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
路径字符串文件应用沙箱路径。

返回值:

类型说明
promise<boolean>promise对象。返回boolean,表示文件是否存在。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
fs.access(filepath).then((res: boolean) => {
  if (res) {
    console.info("file exists");
  } else {
    console.info("file not exists");
  }
}).catch((err: businesserror) => {
  console.error("access failed with error message: " + err.message + ", error code: " + err.code);
});copy to clipboarderrorcopied

fs.access

access(path: string, callback: asynccallback<boolean>): void

检查文件是否存在,使用callback异步回调。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
路径字符串文件应用沙箱路径。
回调asynccallback<boolean>异步检查文件是否存在的回调,如果存在,回调返回true,否则返回false。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
fs.access(filepath, (err: businesserror, res: boolean) => {
  if (err) {
    console.error("access failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    if (res) {
      console.info("file exists");
    } else {
      console.info("file not exists");
    }
  }
});copy to clipboarderrorcopied

fs.accesssync

accesssync(path: string): boolean

以同步方法检查文件是否存在。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
pathstring文件应用沙箱路径。

返回值:

类型说明
boolean返回true,表示文件存在;返回false,表示文件不存在。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
try {
  let res = fs.accesssync(filepath);
  if (res) {
    console.info("file exists");
  } else {
    console.info("file not exists");
  }
} catch(error) {
  let err: businesserror = error as businesserror;
  console.error("accesssync failed with error message: " + err.message + ", error code: " + err.code);
}copy to clipboarderrorcopied

fs.close

close(file: number | file): promise<void>

关闭文件,使用promise异步返回。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
filenumber | file已打开的file对象或已打开的文件描述符fd,关闭后file对象或文件描述符不再具备实际意义,不可再用于进行读写等操作。

返回值:

类型说明
promise<void>promise对象。无返回值。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
let file = fs.opensync(filepath);
fs.close(file).then(() => {
  console.info("close file succeed");
}).catch((err: businesserror) => {
  console.error("close file failed with error message: " + err.message + ", error code: " + err.code);
});copy to clipboarderrorcopied

fs.close

close(file: number | file, callback: asynccallback<void>): void

关闭文件,使用callback异步回调。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
filenumber | file已打开的file对象或已打开的文件描述符fd,关闭后file对象或文件描述符不再具备实际意义,不可再用于进行读写等操作。
callbackasynccallback<void>异步关闭文件之后的回调。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
let file = fs.opensync(filepath);
fs.close(file, (err: businesserror) => {
  if (err) {
    console.error("close file failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("close file succeed");
  }
});copy to clipboarderrorcopied

fs.closesync

closesync(file: number | file): void

以同步方法关闭文件。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
filenumber | file已打开的file对象或已打开的文件描述符fd,关闭后file对象或文件描述符不再具备实际意义,不可再用于进行读写等操作。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let filepath = pathdir + "/test.txt";
let file = fs.opensync(filepath);
fs.closesync(file);copy to clipboarderrorcopied

fs.copyfile

copyfile(src: string | number, dest: string | number, mode?: number): promise<void>

复制文件,使用promise异步返回。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
srcstring | number待复制文件的路径或待复制文件的文件描述符。
deststring | number目标文件路径或目标文件的文件描述符。
modenumbermode提供覆盖文件的选项,当前仅支持0,且默认为0。
0:完全覆盖目标文件,未覆盖部分将被裁切掉。

返回值:

类型说明
promise<void>promise对象。无返回值。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let srcpath = pathdir + "/srcdir/test.txt";
let dstpath = pathdir + "/dstdir/test.txt";
fs.copyfile(srcpath, dstpath).then(() => {
  console.info("copy file succeed");
}).catch((err: businesserror) => {
  console.error("copy file failed with error message: " + err.message + ", error code: " + err.code);
});copy to clipboarderrorcopied

fs.copyfile

copyfile(src: string | number, dest: string | number, mode: number, callback: asynccallback<void>): void

复制文件,可设置覆盖文件的方式,使用callback异步回调。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
srcstring | number待复制文件的路径或待复制文件的文件描述符。
deststring | number目标文件路径或目标文件的文件描述符。
modenumbermode提供覆盖文件的选项,当前仅支持0,且默认为0。
0:完全覆盖目标文件,未覆盖部分将被裁切掉。
callbackasynccallback<void>异步复制文件之后的回调。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let srcpath = pathdir + "/srcdir/test.txt";
let dstpath = pathdir + "/dstdir/test.txt";
fs.copyfile(srcpath, dstpath, 0, (err: businesserror) => {
  if (err) {
    console.error("copy file failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("copy file succeed");
  }
});copy to clipboarderrorcopied

fs.copyfile

copyfile(src: string | number, dest: string | number, callback: asynccallback<void>): void

复制文件,覆盖方式为完全覆盖目标文件,未覆盖部分将被裁切。使用callback异步回调。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
srcstring | number待复制文件的路径或待复制文件的文件描述符。
deststring | number目标文件路径或目标文件的文件描述符。
callbackasynccallback<void>异步复制文件之后的回调。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let srcpath = pathdir + "/srcdir/test.txt";
let dstpath = pathdir + "/dstdir/test.txt";
fs.copyfile(srcpath, dstpath, (err: businesserror) => {
  if (err) {
    console.error("copy file failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("copy file succeed");
  }
});copy to clipboarderrorcopied

fs.copyfilesync

copyfilesync(src: string | number, dest: string | number, mode?: number): void

以同步方法复制文件。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
srcstring | number待复制文件的路径或待复制文件的文件描述符。
deststring | number目标文件路径或目标文件的文件描述符。
modenumbermode提供覆盖文件的选项,当前仅支持0,且默认为0。
0:完全覆盖目标文件,未覆盖部分将被裁切掉。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let srcpath = pathdir + "/srcdir/test.txt";
let dstpath = pathdir + "/dstdir/test.txt";
fs.copyfilesync(srcpath, dstpath);copy to clipboarderrorcopied

fs.copydir10+

copydir(src: string, dest: string, mode?: number): promise<void>

复制源文件夹至目标路径下,使用promise异步返回。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
srcstring源文件夹的应用沙箱路径。
deststring目标文件夹的应用沙箱路径。
modenumber复制模式。默认mode为0。
-  mode为0,文件级别抛异常。目标文件夹下存在与源文件夹名冲突的文件夹,若冲突文件夹下存在同名文件,则抛出异常。源文件夹下未冲突的文件全部移动至目标文件夹下,目标文件夹下未冲突文件将继续保留,且冲突文件信息将在抛出异常的data属性中以array<conflictfiles>形式提供。
-  mode为1,文件级别强制覆盖。目标文件夹下存在与源文件夹名冲突的文件夹,若冲突文件夹下存在同名文件,则强制覆盖冲突文件夹下所有同名文件,未冲突文件将继续保留。

返回值:

类型说明
promise<void>promise对象。无返回值。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
// copy directory from srcpath to destpath
let srcpath = pathdir + "/srcdir/";
let destpath = pathdir + "/destdir/";
fs.copydir(srcpath, destpath, 0).then(() => {
  console.info("copy directory succeed");
}).catch((err: businesserror<array<conflictfiles>>) => {
  if (err.code == 13900015) {
    for (let i = 0; i < err.data.length; i++) {
      console.error("copy directory failed with conflicting files: " + err.data[i].srcfile + " " + err.data[i].destfile);
    }
  } else (err) {
    console.error("copy directory failed with error message: " + err.message + ", error code: " + err.code);
  }
});copy to clipboarderrorcopied

fs.copydir10+

copydir(src: string, dest: string, mode: number, callback: asynccallback<void, array<conflictfiles>>): void

复制源文件夹至目标路径下,可设置复制模式。使用callback异步回调。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
srcstring源文件夹的应用沙箱路径。
deststring目标文件夹的应用沙箱路径。
modenumber复制模式。默认mode为0。
-  mode为0,文件级别抛异常。目标文件夹下存在与源文件夹名冲突的文件夹,若冲突文件夹下存在同名文件,则抛出异常。源文件夹下未冲突的文件全部移动至目标文件夹下,目标文件夹下未冲突文件将继续保留,且冲突文件信息将在抛出异常的data属性中以array<conflictfiles>形式提供。
-  mode为1,文件级别强制覆盖。目标文件夹下存在与源文件夹名冲突的文件夹,若冲突文件夹下存在同名文件,则强制覆盖冲突文件夹下所有同名文件,未冲突文件将继续保留。
callbackasynccallback<void, array<conflictfiles>>异步复制文件夹之后的回调。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
import fs, { conflictfiles } from '@ohos.file.fs';
// copy directory from srcpath to destpath
let srcpath = pathdir + "/srcdir/";
let destpath = pathdir + "/destdir/";
fs.copydir(srcpath, destpath, 0, (err: businesserror<array<conflictfiles>>) => {
  if (err && err.code == 13900015) {
    for (let i = 0; i < err.data.length; i++) {
      console.error("copy directory failed with conflicting files: " + err.data[i].srcfile + " " + err.data[i].destfile);
    }
  } else if (err) {
    console.error("copy directory failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("copy directory succeed");
  }  
});copy to clipboarderrorcopied

fs.copydir10+

copydir(src: string, dest: string, callback: asynccallback<void, array<conflictfiles>>): void

复制源文件夹至目标路径下。使用callback异步回调。

当目标文件夹下存在与源文件夹名冲突的文件夹,若冲突文件夹下存在同名文件,则抛出异常。源文件夹下未冲突的文件全部移动至目标文件夹下,目标文件夹下未冲突文件将继续保留,且冲突文件信息将在抛出异常的data属性中以array<conflictfiles>形式提供。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
srcstring源文件夹的应用沙箱路径。
deststring目标文件夹的应用沙箱路径。
callbackasynccallback<void, array<conflictfiles>>异步复制文件夹之后的回调。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
import fs, { conflictfiles } from '@ohos.file.fs';
// copy directory from srcpath to destpath
let srcpath = pathdir + "/srcdir/";
let destpath = pathdir + "/destdir/";
fs.copydir(srcpath, destpath, (err: businesserror<array<conflictfiles>>) => {
  if (err && err.code == 13900015) {
    for (let i = 0; i < err.data.length; i++) {
      console.error("copy directory failed with conflicting files: " + err.data[i].srcfile + " " + err.data[i].destfile);
    }
  } else if (err) {
    console.error("copy directory failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("copy directory succeed");
  }  
});copy to clipboarderrorcopied

fs.copydirsync10+

copydirsync(src: string, dest: string, mode?: number): void

以同步方法复制源文件夹至目标路径下。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
srcstring源文件夹的应用沙箱路径。
deststring目标文件夹的应用沙箱路径。
modenumber复制模式。默认mode为0。
-  mode为0,文件级别抛异常。目标文件夹下存在与源文件夹名冲突的文件夹,若冲突文件夹下存在同名文件,则抛出异常。源文件夹下未冲突的文件全部移动至目标文件夹下,目标文件夹下未冲突文件将继续保留,且冲突文件信息将在抛出异常的data属性中以array<conflictfiles>形式提供。
-  mode为1,文件级别强制覆盖。目标文件夹下存在与源文件夹名冲突的文件夹,若冲突文件夹下存在同名文件,则强制覆盖冲突文件夹下所有同名文件,未冲突文件将继续保留。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
import fs, { conflictfiles } from '@ohos.file.fs';
// copy directory from srcpath to destpath
let srcpath = pathdir + "/srcdir/";
let destpath = pathdir + "/destdir/";
try {
  fs.copydirsync(srcpath, destpath, 0);
  console.info("copy directory succeed");
} catch (error) {
  let err: businesserror<array<conflictfiles>> = error as businesserror<array<conflictfiles>>;
  if (err.code == 13900015) {
    for (let i = 0; i < err.data.length; i++) {
      console.error("copy directory failed with conflicting files: " + err.data[i].srcfile + " " + err.data[i].destfile);
    }
  } else {
    console.error("copy directory failed with error message: " + err.message + ", error code: " + err.code);
  }
}copy to clipboarderrorcopied

fs.dup10+

dup(fd: number): file

将文件描述符转化为file。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
fdnumber文件描述符。

返回值:

类型说明
file打开的file对象。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

// convert fd to file
let fd: number = 0;  // fd comes from other modules
let file = fs.dup(fd);
console.info("the name of the file is " + file.name);
fs.closesync(file);copy to clipboarderrorcopied

fs.mkdir

mkdir(path: string): promise<void>

创建目录,使用promise异步返回。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
pathstring目录的应用沙箱路径。

返回值:

类型说明
promise<void>promise对象。无返回值。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let dirpath = pathdir + "/testdir";
fs.mkdir(dirpath).then(() => {
  console.info("mkdir succeed");
}).catch((err: businesserror) => {
  console.error("mkdir failed with error message: " + err.message + ", error code: " + err.code);
});copy to clipboarderrorcopied

fs.mkdir

mkdir(path: string, callback: asynccallback<void>): void

创建目录,使用callback异步回调。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
pathstring目录的应用沙箱路径。
callbackasynccallback<void>异步创建目录操作完成之后的回调。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let dirpath = pathdir + "/testdir";
fs.mkdir(dirpath, (err: businesserror) => {
  if (err) {
    console.error("mkdir failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("mkdir succeed");
  }
});copy to clipboarderrorcopied

fs.mkdirsync

mkdirsync(path: string): void

以同步方法创建目录。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
pathstring目录的应用沙箱路径。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let dirpath = pathdir + "/testdir";
fs.mkdirsync(dirpath);copy to clipboarderrorcopied

fs.open

open(path: string, mode?: number): promise<file>

打开文件,使用promise异步返回。支持使用uri打开文件。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
pathstring文件的应用沙箱路径或文件uri。
modenumber打开文件的选项,必须指定如下选项中的一个,默认以只读方式打开:
- openmode.read_only(0o0):只读打开。
- openmode.write_only(0o1):只写打开。
- openmode.read_write(0o2):读写打开。
给定如下功能选项,以按位或的方式追加,默认不给定任何额外选项:
- openmode.create(0o100):若文件不存在,则创建文件。
- openmode.trunc(0o1000):如果文件存在且以只写或读写的方式打开文件,则将其长度裁剪为零。
- openmode.append(0o2000):以追加方式打开,后续写将追加到文件末尾。
- openmode.nonblock(0o4000):如果path指向fifo、块特殊文件或字符特殊文件,则本次打开及后续 io 进行非阻塞操作。
- openmode.dir(0o200000):如果path不指向目录,则出错。不允许附加写权限。
- openmode.nofollow(0o400000):如果path指向符号链接,则出错。
- openmode.sync(0o4010000):以同步io的方式打开文件。

返回值:

类型说明
promise<file>promise对象。返回file对象。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
fs.open(filepath, fs.openmode.read_write | fs.openmode.create).then((file: fs.file) => {
  console.info("file fd: " + file.fd);
  fs.closesync(file);
}).catch((err: businesserror) => {
  console.error("open file failed with error message: " + err.message + ", error code: " + err.code);
});copy to clipboarderrorcopied

fs.open

open(path: string, mode: number, callback: asynccallback<file>): void

打开文件,可设置打开文件的选项。使用callback异步回调。

支持使用uri打开文件。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
pathstring文件的应用沙箱路径或uri。
modenumber打开文件的选项,必须指定如下选项中的一个,默认以只读方式打开:
- openmode.read_only(0o0):只读打开。
- openmode.write_only(0o1):只写打开。
- openmode.read_write(0o2):读写打开。
给定如下功能选项,以按位或的方式追加,默认不给定任何额外选项:
- openmode.create(0o100):若文件不存在,则创建文件。
- openmode.trunc(0o1000):如果文件存在且以只写或读写的方式打开文件,则将其长度裁剪为零。
- openmode.append(0o2000):以追加方式打开,后续写将追加到文件末尾。
- openmode.nonblock(0o4000):如果path指向fifo、块特殊文件或字符特殊文件,则本次打开及后续 io 进行非阻塞操作。
- openmode.dir(0o200000):如果path不指向目录,则出错。不允许附加写权限。
- openmode.nofollow(0o400000):如果path指向符号链接,则出错。
- openmode.sync(0o4010000):以同步io的方式打开文件。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
fs.open(filepath, fs.openmode.read_write | fs.openmode.create, (err: businesserror, file: fs.file) => {
  if (err) {
    console.error("open failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("file fd: " + file.fd);
  }
  fs.closesync(file);
});copy to clipboarderrorcopied

fs.open

open(path: string, callback: asynccallback<file>): void

以只读模式打开文件。使用callback异步回调。

支持使用uri打开文件。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
pathstring文件的应用沙箱路径或uri。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
fs.open(filepath, (err: businesserror, file: fs.file) => {
  if (err) {
    console.error("open failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("file fd: " + file.fd);
  }
  fs.closesync(file);
});copy to clipboarderrorcopied

fs.opensync

opensync(path: string, mode?: number): file

以同步方法打开文件。支持使用uri打开文件。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
pathstring打开文件的应用沙箱路径或uri。
modenumber打开文件的选项,必须指定如下选项中的一个,默认以只读方式打开:
- openmode.read_only(0o0):只读打开。
- openmode.write_only(0o1):只写打开。
- openmode.read_write(0o2):读写打开。
给定如下功能选项,以按位或的方式追加,默认不给定任何额外选项:
- openmode.create(0o100):若文件不存在,则创建文件。
- openmode.trunc(0o1000):如果文件存在且以只写或读写的方式打开文件,则将其长度裁剪为零。
- openmode.append(0o2000):以追加方式打开,后续写将追加到文件末尾。
- openmode.nonblock(0o4000):如果path指向fifo、块特殊文件或字符特殊文件,则本次打开及后续 io 进行非阻塞操作。
- openmode.dir(0o200000):如果path不指向目录,则出错。不允许附加写权限。
- openmode.nofollow(0o400000):如果path指向符号链接,则出错。
- openmode.sync(0o4010000):以同步io的方式打开文件。

返回值:

类型说明
file打开的file对象。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let filepath = pathdir + "/test.txt";
let file = fs.opensync(filepath, fs.openmode.read_write | fs.openmode.create);
console.info("file fd: " + file.fd);
fs.closesync(file);copy to clipboarderrorcopied

fs.read

read(fd: number, buffer: arraybuffer, options?: { offset?: number; length?: number; }): promise<number>

从文件读取数据,使用promise异步返回。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
fdnumber已打开的文件描述符。
bufferarraybuffer用于保存读取到的文件数据的缓冲区。
optionsobject支持如下选项:
- offset,number类型,表示期望读取文件的位置。可选,默认从当前位置开始读。
- length,number类型,表示期望读取数据的长度。可选,默认缓冲区长度。

返回值:

类型说明
promise<number>promise对象。返回读取的实际长度。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
import buffer from '@ohos.buffer';
let filepath = pathdir + "/test.txt";
let file = fs.opensync(filepath, fs.openmode.read_write);
let arraybuffer = new arraybuffer(4096);
fs.read(file.fd, arraybuffer).then((readlen: number) => {
  console.info("read file data succeed");
  let buf = buffer.from(arraybuffer, 0, readlen);
  console.info(`the content of file: ${buf.tostring()}`);
}).catch((err: businesserror) => {
  console.error("read file data failed with error message: " + err.message + ", error code: " + err.code);
}).finally(() => {
  fs.closesync(file);
});copy to clipboarderrorcopied

fs.read

read(fd: number, buffer: arraybuffer, options?: { offset?: number; length?: number; }, callback: asynccallback<number>): void

从文件读取数据,使用callback异步回调。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
fdnumber已打开的文件描述符。
bufferarraybuffer用于保存读取到的文件数据的缓冲区。
optionsobject支持如下选项:
- offset,number类型,表示期望读取文件的位置。可选,默认从当前位置开始读。
- length,number类型,表示期望读取数据的长度。可选,默认缓冲区长度。
callbackasynccallback<number>异步读取数据之后的回调。返回读取的实际长度。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
import buffer from '@ohos.buffer';
let filepath = pathdir + "/test.txt";
let file = fs.opensync(filepath, fs.openmode.read_write);
let arraybuffer = new arraybuffer(4096);
fs.read(file.fd, arraybuffer, (err: businesserror, readlen: number) => {
  if (err) {
    console.error("read failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("read file data succeed");
    let buf = buffer.from(arraybuffer, 0, readlen);
    console.info(`the content of file: ${buf.tostring()}`);
  }
  fs.closesync(file);
});copy to clipboarderrorcopied

fs.readsync

readsync(fd: number, buffer: arraybuffer, options?: { offset?: number; length?: number; }): number

以同步方法从文件读取数据。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
fdnumber已打开的文件描述符。
bufferarraybuffer用于保存读取到的文件数据的缓冲区。
optionsobject支持如下选项:
- offset,number类型,表示期望读取文件的位置。可选,默认从当前位置开始读。
- length,number类型,表示期望读取数据的长度。可选,默认缓冲区长度。

返回值:

类型说明
number返回实际读取的长度。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let filepath = pathdir + "/test.txt";
let file = fs.opensync(filepath, fs.openmode.read_write);
let buf = new arraybuffer(4096);
fs.readsync(file.fd, buf);
fs.closesync(file);copy to clipboarderrorcopied

fs.rmdir

rmdir(path: string): promise<void>

删除整个目录,使用promise异步返回。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
pathstring目录的应用沙箱路径。

返回值:

类型说明
promise<void>promise对象。无返回值。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let dirpath = pathdir + "/testdir";
fs.rmdir(dirpath).then(() => {
  console.info("rmdir succeed");
}).catch((err: businesserror) => {
  console.error("rmdir failed with error message: " + err.message + ", error code: " + err.code);
});copy to clipboarderrorcopied

fs.rmdir

rmdir(path: string, callback: asynccallback<void>): void

删除整个目录,使用callback异步回调。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
pathstring目录的应用沙箱路径。
callbackasynccallback<void>异步删除目录之后的回调。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let dirpath = pathdir + "/testdir";
fs.rmdir(dirpath, (err: businesserror) => {
  if (err) {
    console.error("rmdir failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("rmdir succeed");
  }
});copy to clipboarderrorcopied

fs.rmdirsync

rmdirsync(path: string): void

以同步方法删除目录。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
pathstring目录的应用沙箱路径。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let dirpath = pathdir + "/testdir";
fs.rmdirsync(dirpath);copy to clipboarderrorcopied

unlink(path: string): promise<void>

删除单个文件,使用promise异步返回。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
pathstring文件的应用沙箱路径。

返回值:

类型说明
promise<void>promise对象。无返回值。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
fs.unlink(filepath).then(() => {
  console.info("remove file succeed");
}).catch((err: businesserror) => {
  console.error("remove file failed with error message: " + err.message + ", error code: " + err.code);
});复制到剪贴板错误复制

unlink(path: string, callback: asynccallback<void>): void

删除文件,使用callback异步回调。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
路径字符串文件的应用沙箱路径。
回调asynccallback<void>异步删除文件之后的回调。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
fs.unlink(filepath, (err: businesserror) => {
  if (err) {
    console.error("remove file failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("remove file succeed");
  }
});复制到剪贴板错误复制

fs.unlinksync

unlinksync(path: string): void

以同步方法删除文件。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
路径字符串文件的应用沙箱路径。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let filepath = pathdir + "/test.txt";
fs.unlinksync(filepath);复制到剪贴板错误复制

fs.write

write(fd: number, buffer: arraybuffer | string, options?: { offset?: number; length?: number; encoding?: string; }): promise<number>

将数据写入文件,使用promise异步返回。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
fd已打开的文件描述符。
bufferarraybuffer | string待写入文件的数据,可来自缓冲区或字符串。
选项object支持如下选项:
- offset,number类型,表示期望写入文件的位置。可选,默认从当前位置开始写。
- length,number类型,表示期望写入数据的长度。可选,默认缓冲区长度。
- encoding,string类型,当数据是string类型时有效,表示数据的编码方式,默认 'utf-8'。当前仅支持 'utf-8'。

返回值:

类型说明
promise<number>promise对象。返回实际写入的长度。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
let file = fs.opensync(filepath, fs.openmode.read_write | fs.openmode.create);
let str: string = "hello, world";
fs.write(file.fd, str).then((writelen: number) => {
  console.info("write data to file succeed and size is:" + writelen);
}).catch((err: businesserror) => {
  console.error("write data to file failed with error message: " + err.message + ", error code: " + err.code);
}).finally(() => {
  fs.closesync(file);
});copy to clipboarderrorcopied

fs.write

write(fd: number, buffer: arraybuffer | string, options?: { offset?: number; length?: number; encoding?: string; }, callback: asynccallback<number>): void

将数据写入文件,使用callback异步回调。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
fdnumber已打开的文件描述符。
bufferarraybuffer | string待写入文件的数据,可来自缓冲区或字符串。
optionsobject支持如下选项:
- offset,number类型,表示期望写入文件的位置。可选,默认从当前位置开始写。
- length,number类型,表示期望写入数据的长度。可选,默认缓冲区长度。
- encoding,string类型,当数据是string类型时有效,表示数据的编码方式,默认 'utf-8'。当前仅支持 'utf-8'。
callbackasynccallback<number>异步将数据写入完成后执行的回调函数。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
let file = fs.opensync(filepath, fs.openmode.read_write | fs.openmode.create);
let str: string = "hello, world";
fs.write(file.fd, str, (err: businesserror, writelen: number) => {
  if (err) {
    console.error("write data to file failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("write data to file succeed and size is:" + writelen);
  }
  fs.closesync(file);
});copy to clipboarderrorcopied

fs.writesync

writesync(fd: number, buffer: arraybuffer | string, options?: { offset?: number; length?: number; encoding?: string; }): number

以同步方法将数据写入文件。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
fdnumber已打开的文件描述符。
bufferarraybuffer | string待写入文件的数据,可来自缓冲区或字符串。
optionsobject支持如下选项:
- offset,number类型,表示期望写入文件的位置。可选,默认从当前位置开始写。
- length,number类型,表示期望写入数据的长度。可选,默认缓冲区长度。
- encoding,string类型,当数据是string类型时有效,表示数据的编码方式,默认 'utf-8'。当前仅支持 'utf-8'。

返回值:

类型说明
实际写入的长度。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let filepath = pathdir + "/test.txt";
let file = fs.opensync(filepath, fs.openmode.read_write | fs.openmode.create);
let str: string = "hello, world";
let writelen = fs.writesync(file.fd, str);
console.info("write data to file succeed and size is:" + writelen);
fs.closesync(file);复制到剪贴板错误复制

fs.truncate

truncate(file: string | number, len?: number): promise<void>

截断文件,使用promise异步返回。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
filestring | number文件的应用沙箱路径或已打开的文件描述符fd。
len文件截断后的长度,以字节为单位。默认为0。

返回值:

类型说明
承诺<无效>promise对象。无返回值。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
let len: number = 5;
fs.truncate(filepath, len).then(() => {
  console.info("truncate file succeed");
}).catch((err: businesserror) => {
  console.error("truncate file failed with error message: " + err.message + ", error code: " + err.code);
});复制到剪贴板错误复制

fs.truncate

truncate(file: string | number, len?: number, callback: asynccallback<void>): void

截断文件,使用callback异步回调。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
filestring | number文件的应用沙箱路径或已打开的文件描述符fd。
len文件截断后的长度,以字节为单位。默认为0。
回调asynccallback<void>回调函数,本调用无返回值。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
let len: number = 5;
fs.truncate(filepath, len, (err: businesserror) => {
  if (err) {
    console.error("truncate failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("truncate succeed");
  }
});复制到剪贴板错误复制

fs.truncatesync

truncatesync(file: string | number, len?: number): void

以同步方法截断文件。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
filestring | number文件的应用沙箱路径或已打开的文件描述符fd。
lennumber文件截断后的长度,以字节为单位。默认为0。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let filepath = pathdir + "/test.txt";
let len: number = 5;
fs.truncatesync(filepath, len);copy to clipboarderrorcopied

fs.readtext

readtext(filepath: string, options?: { offset?: number; length?: number; encoding?: string; }): promise<string>

基于文本方式读取文件(即直接读取文件的文本内容),使用promise异步返回。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
filepathstring文件的应用沙箱路径。
optionsobject支持如下选项:
- offset,number类型,表示期望读取文件的位置。可选,默认从当前位置开始读取。
- length,number类型,表示期望读取数据的长度。可选,默认文件长度。
- encoding,string类型,当数据是 string 类型时有效,表示数据的编码方式,默认 'utf-8',仅支持 'utf-8'。

返回值:

类型说明
promise<string>promise对象。返回读取文件的内容。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
fs.readtext(filepath).then((str: string) => {
  console.info("readtext succeed:" + str);
}).catch((err: businesserror) => {
  console.error("readtext failed with error message: " + err.message + ", error code: " + err.code);
});copy to clipboarderrorcopied

fs.readtext

readtext(filepath: string, options?: { offset?: number; length?: number; encoding?: string; }, callback: asynccallback<string>): void

基于文本方式读取文件(即直接读取文件的文本内容),使用callback异步回调。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
filepathstring文件的应用沙箱路径。
optionsobject支持如下选项:
- offset,number类型,表示期望读取文件的位置。可选,默认从当前位置开始读取。
- length,number类型,表示期望读取数据的长度。可选,默认文件长度。
- encoding,string类型,表示数据的编码方式,默认 'utf-8',仅支持 'utf-8'。
callbackasynccallback<string>回调函数,返回读取文件的内容。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
class option {
  offset: number = 0;
  length: number = 0;
  encoding: string = 'utf-8';
}
let stat = fs.statsync(filepath);
let option = new option();
option.offset = 1;
option.length = stat.size;
fs.readtext(filepath, option, (err: businesserror, str: string) => {
  if (err) {
    console.error("readtext failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("readtext succeed:" + str);
  }
});copy to clipboarderrorcopied

fs.readtextsync

readtextsync(filepath: string, options?: { offset?: number; length?: number; encoding?: string; }): string

以同步方法基于文本方式读取文件(即直接读取文件的文本内容)。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
filepathstring文件的应用沙箱路径。
optionsobject支持如下选项:
- offset,number类型,表示期望读取文件的位置。可选,默认从当前位置开始读取。
- length,number类型,表示期望读取数据的长度。可选,默认文件长度。
- encoding,string类型,当数据是 string 类型时有效,表示数据的编码方式,默认 'utf-8',仅支持 'utf-8'。

返回值:

类型说明
string返回读取文件的内容。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let filepath = pathdir + "/test.txt";
class option {
  offset: number = 0;
  length: number = 0;
  encoding: string = 'utf-8';
}
let stat = fs.statsync(filepath);
let option = new option();
option.offset = 1;
option.length = stat.size;
let str = fs.readtextsync(filepath, option);
console.info("readtext succeed:" + str);复制到剪贴板错误复制

fs.lstat

lstat(path: string): promise<stat>

获取链接文件信息,使用promise异步返回。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
路径字符串文件的应用沙箱路径。

返回值:

类型说明
promise<stat>promise对象,返回文件对象,表示文件的具体信息,详情见stat。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/linktofile";
fs.lstat(filepath).then((stat: fs.stat) => {
  console.info("lstat succeed, the size of file is " + stat.size);
}).catch((err: businesserror) => {
  console.error("lstat failed with error message: " + err.message + ", error code: " + err.code);
});复制到剪贴板错误复制

fs.lstat

lstat(path: string, callback: asynccallback<stat>): void

获取链接文件信息,使用callback异步回调。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
路径字符串文件的应用沙箱路径。
回调asynccallback<stat>回调函数,返回文件的具体信息。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/linktofile";
fs.lstat(filepath, (err: businesserror, stat: fs.stat) => {
  if (err) {
    console.error("lstat failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("lstat succeed, the size of file is " + stat.size);
  }
});复制到剪贴板错误复制

fs.lstatsync

lstatsync(path: string): stat

以同步方法获取链接文件信息。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
pathstring文件的应用沙箱路径。

返回值:

类型说明
stat表示文件的具体信息。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let filepath = pathdir + "/linktofile";
fs.lstatsync(filepath);copy to clipboarderrorcopied

fs.rename

rename(oldpath: string, newpath: string): promise<void>

重命名文件或文件夹,使用promise异步返回。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
oldpathstring文件的应用沙箱原路径。
newpathstring文件的应用沙箱新路径。

返回值:

类型说明
promise<void>promise对象。无返回值。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let srcfile = pathdir + "/test.txt";
let dstfile = pathdir + "/new.txt";
fs.rename(srcfile, dstfile).then(() => {
  console.info("rename succeed");
}).catch((err: businesserror) => {
  console.error("rename failed with error message: " + err.message + ", error code: " + err.code);
});copy to clipboarderrorcopied

fs.rename

rename(oldpath: string, newpath: string, callback: asynccallback<void>): void

重命名文件或文件夹,使用callback异步回调。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
oldpathstring文件的应用沙箱原路径。
newpathstring文件的应用沙箱新路径。
callbackasynccallback<void>异步重命名文件之后的回调。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let srcfile = pathdir + "/test.txt";
let dstfile = pathdir + "/new.txt";
fs.rename(srcfile, dstfile, (err: businesserror) => {
  if (err) {
    console.error("rename failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("rename succeed");
  }
});copy to clipboarderrorcopied

fs.renamesync

renamesync(oldpath: string, newpath: string): void

以同步方法重命名文件或文件夹。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
oldpathstring文件的应用沙箱原路径。
newpathstring文件的应用沙箱新路径。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let srcfile = pathdir + "/test.txt";
let dstfile = pathdir + "/new.txt";
fs.renamesync(srcfile, dstfile);copy to clipboarderrorcopied

fs.fsync

fsync(fd: number): promise<void>

同步文件数据,使用promise异步返回。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
fdnumber已打开的文件描述符。

返回值:

类型说明
promise<void>promise对象。无返回值。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
let file = fs.opensync(filepath);
fs.fsync(file.fd).then(() => {
  console.info("sync data succeed");
}).catch((err: businesserror) => {
  console.error("sync data failed with error message: " + err.message + ", error code: " + err.code);
}).finally(() => {
  fs.closesync(file);
});copy to clipboarderrorcopied

fs.fsync

fsync(fd: number, callback: asynccallback<void>): void

同步文件数据,使用callback异步回调。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
fdnumber已打开的文件描述符。
callbackasynccallback<void>异步将文件数据同步之后的回调。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
let file = fs.opensync(filepath);
fs.fsync(file.fd, (err: businesserror) => {
  if (err) {
    console.error("fsync failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("fsync succeed");
  }
  fs.closesync(file);
});copy to clipboarderrorcopied

fs.fsyncsync

fsyncsync(fd: number): void

以同步方法同步文件数据。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
fdnumber已打开的文件描述符。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let filepath = pathdir + "/test.txt";
let file = fs.opensync(filepath);
fs.fsyncsync(file.fd);
fs.closesync(file);copy to clipboarderrorcopied

fs.fdatasync

fdatasync(fd: number): promise<void>

实现文件内容数据同步,使用promise异步返回。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
fdnumber已打开的文件描述符。

返回值:

类型说明
promise<void>promise对象。无返回值。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
let file = fs.opensync(filepath);
fs.fdatasync(file.fd).then(() => {
  console.info("sync data succeed");
}).catch((err: businesserror) => {
  console.error("sync data failed with error message: " + err.message + ", error code: " + err.code);
}).finally(() => {
  fs.closesync(file);
});copy to clipboarderrorcopied

fs.fdatasync

fdatasync(fd: number, callback: asynccallback<void>): void

实现文件内容数据同步,使用callback异步回调。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
fdnumber已打开的文件描述符。
callbackasynccallback<void>异步将文件内容数据同步之后的回调。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
let file = fs.opensync(filepath);
fs.fdatasync (file.fd, (err: businesserror) => {
  if (err) {
    console.error("fdatasync failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("fdatasync succeed");
  }
  fs.closesync(file);
});copy to clipboarderrorcopied

fs.fdatasyncsync

fdatasyncsync(fd: number): void

以同步方法实现文件内容数据同步。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
fdnumber已打开的文件描述符。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let filepath = pathdir + "/test.txt";
let file = fs.opensync(filepath);
fs.fdatasyncsync(file.fd);
fs.closesync(file);copy to clipboarderrorcopied

symlink(target: string, srcpath: string): promise<void>

基于文件路径创建符号链接,使用promise异步返回。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
targetstring源文件的应用沙箱路径。
srcpathstring符号链接文件的应用沙箱路径。

返回值:

类型说明
promise<void>promise对象。无返回值。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let srcfile = pathdir + "/test.txt";
let dstfile = pathdir + "/test";
fs.symlink(srcfile, dstfile).then(() => {
  console.info("symlink succeed");
}).catch((err: businesserror) => {
  console.error("symlink failed with error message: " + err.message + ", error code: " + err.code);
});copy to clipboarderrorcopied

symlink(target: string, srcpath: string, callback: asynccallback<void>): void

基于文件路径创建符号链接,使用callback异步回调。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
targetstring源文件的应用沙箱路径。
srcpathstring符号链接文件的应用沙箱路径。
callbackasynccallback<void>异步创建符号链接信息之后的回调。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let srcfile = pathdir + "/test.txt";
let dstfile = pathdir + "/test";
fs.symlink(srcfile, dstfile, (err: businesserror) => {
  if (err) {
    console.error("symlink failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("symlink succeed");
  }
});copy to clipboarderrorcopied

fs.symlinksync

symlinksync(target: string, srcpath: string): void

以同步的方法基于文件路径创建符号链接。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
targetstring源文件的应用沙箱路径。
srcpathstring符号链接文件的应用沙箱路径。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let srcfile = pathdir + "/test.txt";
let dstfile = pathdir + "/test";
fs.symlinksync(srcfile, dstfile);copy to clipboarderrorcopied

fs.listfile

listfile(path: string, options?: { recursion?: boolean; listnum?: number; filter?: filter; }): promise<string[]>

列出文件夹下所有文件名,支持递归列出所有文件名(包含子目录下),支持文件过滤,使用promise异步返回。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
pathstring文件夹的应用沙箱路径。
optionsobject文件过滤选项。默认不进行过滤。

options参数说明:

参数名类型必填说明
recursionboolean是否递归子目录下文件名,默认为false。当recursion为false时,返回当前目录下满足过滤要求的文件名及文件夹名。当recursion为true时,返回此目录下所有满足过滤要求的文件的相对路径(以/开头)。
listnumnumber列出文件名数量。当设置0时,列出所有文件,默认为0。
filterfilter文件过滤选项。当前仅支持后缀名匹配、文件名模糊查询、文件大小过滤、最近修改时间过滤。

返回值:

类型说明
promise<string[]>promise对象。返回文件名数组。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
import fs, { filter } from '@ohos.file.fs';
class listfileoption {
  public recursion: boolean = false;
  public listnum: number = 0;
  public filter: filter = {};
}
let option = new listfileoption();
option.filter.suffix = [".png", ".jpg", ".jpeg"];
option.filter.displayname = ["*abc", "efg*"];
option.filter.filesizeover = 1024;
option.filter.lastmodifiedafter = new date().gettime();
fs.listfile(pathdir, option).then((filenames: array<string>) => {
  console.info("listfile succeed");
  for (let i = 0; i < filenames.length; i++) {
    console.info("filename: %s", filenames[i]);
  }
}).catch((err: businesserror) => {
  console.error("list file failed with error message: " + err.message + ", error code: " + err.code);
});复制到剪贴板错误复制

fs.listfile

listfile(path: string, options?: { recursion?: boolean; listnum?: number; filter?: filter; }, callback: asynccallback<string[]>): void

列出文件夹下所有文件名,支持递归列出所有文件名(包含子目录下),支持文件过滤,使用callback异步回调。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
路径字符串文件夹的应用沙箱路径。
选项object文件过滤选项。默认不进行过滤。
回调asynccallback<string[]>异步列出文件名数组之后的回调。

options参数说明:

参数名类型必填说明
recursion布尔是否递归子目录下文件名,默认为false。当recursion为false时,返回当前目录下满足过滤要求的文件名及文件夹名。当recursion为true时,返回此目录下所有满足过滤要求的文件的相对路径(以/开头)。
listnum列出文件名数量。当设置0时,列出所有文件,默认为0。
filterfilter文件过滤选项。当前仅支持后缀名匹配、文件名模糊查询、文件大小过滤、最近修改时间过滤。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
import fs, { filter } from '@ohos.file.fs';
class listfileoption {
  public recursion: boolean = false;
  public listnum: number = 0;
  public filter: filter = {};
}
let option = new listfileoption();
option.filter.suffix = [".png", ".jpg", ".jpeg"];
option.filter.displayname = ["*abc", "efg*"];
option.filter.filesizeover = 1024;
option.filter.lastmodifiedafter = new date().gettime();
fs.listfile(pathdir, option, (err: businesserror, filenames: array<string>) => {
  if (err) {
    console.error("list file failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("listfile succeed");
    for (let i = 0; i < filenames.length; i++) {
      console.info("filename: %s", filenames[i]);
    }
  }
});复制到剪贴板错误复制

fs.listfilesync

listfilesync(path: string, options?: { recursion?: boolean; listnum?: number; filter?: filter; }): string[]

以同步方式列出文件夹下所有文件名,支持递归列出所有文件名(包含子目录下),支持文件过滤。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
pathstring文件夹的应用沙箱路径。
optionsobject文件过滤选项。默认不进行过滤。

options参数说明:

参数名类型必填说明
recursionboolean是否递归子目录下文件名,默认为false。当recursion为false时,返回当前目录下满足过滤要求的文件名及文件夹名。当recursion为true时,返回此目录下所有满足过滤要求的文件的相对路径(以/开头)。
listnumnumber列出文件名数量。当设置0时,列出所有文件,默认为0。
filterfilter文件过滤选项。当前仅支持后缀名匹配、文件名模糊查询、文件大小过滤、最近修改时间过滤。

返回值:

类型说明
string[]返回文件名数组。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import fs, { filter } from '@ohos.file.fs';
class listfileoption {
  public recursion: boolean = false;
  public listnum: number = 0;
  public filter: filter = {};
}
let option = new listfileoption();
option.filter.suffix = [".png", ".jpg", ".jpeg"];
option.filter.displayname = ["*abc", "efg*"];
option.filter.filesizeover = 1024;
option.filter.lastmodifiedafter = new date().gettime();
let filenames = fs.listfilesync(pathdir, option);
console.info("listfile succeed");
for (let i = 0; i < filenames.length; i++) {
  console.info("filename: %s", filenames[i]);
}copy to clipboarderrorcopied

fs.movedir10+

movedir(src: string, dest: string, mode?: number): promise<void>

移动源文件夹至目标路径下,使用promise异步返回。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
srcstring源文件夹的应用沙箱路径。
deststring目标文件夹的应用沙箱路径。
modenumber移动模式。默认mode为0。
- mode为0,文件夹级别抛异常。若目标文件夹下存在与源文件夹名冲突的非空文件夹,则抛出异常。
- mode为1,文件级别抛异常。目标文件夹下存在与源文件夹名冲突的文件夹,若冲突文件夹下存在同名文件,则抛出异常。源文件夹下未冲突的文件全部移动至目标文件夹下,目标文件夹下未冲突文件将继续保留,且冲突文件信息将在抛出异常的data属性中以array<conflictfiles>形式提供。
-  mode为2,文件级别强制覆盖。目标文件夹下存在与源文件夹名冲突的文件夹,若冲突文件夹下存在同名文件,则强制覆盖冲突文件夹下所有同名文件,未冲突文件将继续保留。
-  mode为3,文件夹级别强制覆盖。移动源文件夹至目标文件夹下,目标文件夹下移动的文件夹内容与源文件夹完全一致。若目标文件夹下存在与源文件夹名冲突的文件夹,该文件夹下所有原始文件将不会保留。

返回值:

类型说明
promise<void>promise对象。无返回值。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
// move directory from srcpath to destpath
let srcpath = pathdir + "/srcdir/";
let destpath = pathdir + "/destdir/";
fs.movedir(srcpath, destpath, 1).then(() => {
  console.info("move directory succeed");
}).catch((err: businesserror<array<conflictfiles>) => {
  if (err.code == 13900015) {
    for (let i = 0; i < err.data.length; i++) {
      console.error("copy directory failed with conflicting files: " + err.data[i].srcfile + " " + err.data[i].destfile);
    }
  } else(err) {
    console.error("copy directory failed with error message: " + err.message + ", error code: " + err.code);
  }
});copy to clipboarderrorcopied

fs.movedir10+

movedir(src: string, dest: string, mode: number, callback: asynccallback<void, array<conflictfiles>>): void

移动源文件夹至目标路径下,支持设置移动模式。使用callback异步回调。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
srcstring源文件夹的应用沙箱路径。
deststring目标文件夹的应用沙箱路径。
模式移动模式。默认mode为0。
- mode为0,文件夹级别抛异常。若目标文件夹下存在与源文件夹名冲突的文件夹,则抛出异常。
- mode为1,文件级别抛异常。目标文件夹下存在与源文件夹名冲突的文件夹,若冲突文件夹下存在同名文件,则抛出异常。源文件夹下未冲突的文件全部移动至目标文件夹下,目标文件夹下未冲突文件将继续保留,且冲突文件信息将在抛出异常的data属性中以array<conflictfiles>形式提供。
-  mode为2,文件级别强制覆盖。目标文件夹下存在与源文件夹名冲突的文件夹,若冲突文件夹下存在同名文件,则强制覆盖冲突文件夹下所有同名文件,未冲突文件将继续保留。
-  mode为3,文件夹级别强制覆盖。移动源文件夹至目标文件夹下,目标文件夹下移动的文件夹内容与源文件夹完全一致。若目标文件夹下存在与源文件夹名冲突的文件夹,该文件夹下所有原始文件将不会保留。
回调asynccallback<void, array<conflictfiles>>异步移动文件夹之后的回调。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
import fs, { conflictfiles } from '@ohos.file.fs';
// move directory from srcpath to destpath
let srcpath = pathdir + "/srcdir/";
let destpath = pathdir + "/destdir/";
fs.movedir(srcpath, destpath, 1, (err: businesserror<array<conflictfiles>>) => {
  if (err && err.code == 13900015) {
    for (let i = 0; i < err.data.length; i++) {
      console.error("move directory failed with conflicting files: " + err.data[i].srcfile + " " + err.data[i].destfile);
    }
  } else if (err) {
    console.error("move directory failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("move directory succeed");
  }  
});复制到剪贴板错误复制

fs.movedir10+

movedir(src: string, dest: string, callback: asynccallback<void, array<conflictfiles>>): void

移动源文件夹至目标路径下。使用callback异步回调。

移动模式为文件夹级别抛异常,当目标文件夹下存在与源文件夹名冲突的文件夹,则抛出异常。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
src字符串源文件夹的应用沙箱路径。
dest字符串目标文件夹的应用沙箱路径。
回调asynccallback<void, array<conflictfiles>>异步移动文件夹之后的回调。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
import fs, { conflictfiles } from '@ohos.file.fs';
// move directory from srcpath to destpath
let srcpath = pathdir + "/srcdir/";
let destpath = pathdir + "/destdir/";
fs.movedir(srcpath, destpath, (err: businesserror<array<conflictfiles>>) => {
  if (err && err.code == 13900015) {
    for (let i = 0; i < err.data.length; i++) {
      console.error("move directory failed with conflicting files: " + err.data[i].srcfile + " " + err.data[i].destfile);
    }
  } else if (err) {
    console.error("move directory failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("move directory succeed");
  }  
});复制到剪贴板错误复制

fs.movedirsync10+

movedirsync(src: string, dest: string, mode?: number): void

以同步方法移动源文件夹至目标路径下。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
src字符串源文件夹的应用沙箱路径。
deststring目标文件夹的应用沙箱路径。
modenumber移动模式。默认mode为0。
- mode为0,文件夹级别抛异常。若目标文件夹下存在与源文件夹名冲突的文件夹,则抛出异常。
- mode为1,文件级别抛异常。目标文件夹下存在与源文件夹名冲突的文件夹,若冲突文件夹下存在同名文件,则抛出异常。源文件夹下未冲突的文件全部移动至目标文件夹下,目标文件夹下未冲突文件将继续保留,且冲突文件信息将在抛出异常的data属性中以array<conflictfiles>形式提供。
-  mode为2,文件级别强制覆盖。目标文件夹下存在与源文件夹名冲突的文件夹,若冲突文件夹下存在同名文件,则强制覆盖冲突文件夹下所有同名文件,未冲突文件将继续保留。
-  mode为3,文件夹级别强制覆盖。移动源文件夹至目标文件夹下,目标文件夹下移动的文件夹内容与源文件夹完全一致。若目标文件夹下存在与源文件夹名冲突的文件夹,该文件夹下所有原始文件将不会保留。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
import fs, { conflictfiles } from '@ohos.file.fs';
// move directory from srcpath to destpath
let srcpath = pathdir + "/srcdir/";
let destpath = pathdir + "/destdir/";
try {
  fs.movedirsync(srcpath, destpath, 1);
  console.info("move directory succeed");
} catch (error) {
  let err: businesserror<array<conflictfiles>> = error as businesserror<array<conflictfiles>>;
  if (err.code == 13900015) {
    for (let i = 0; i < err.data.length; i++) {
      console.error("move directory failed with conflicting files: " + err.data[i].srcfile + " " + err.data[i].destfile);
    }
  } else {
    console.error("move directory failed with error message: " + err.message + ", error code: " + err.code);
  }
}copy to clipboarderrorcopied

fs.movefile

movefile(src: string, dest: string, mode?: number): promise<void>

移动文件,使用promise异步返回。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
srcstring源文件的应用沙箱路径。
deststring目的文件的应用沙箱路径。
modenumber移动模式。若mode为0,移动位置存在同名文件时,强制移动覆盖。若mode为1,移动位置存在同名文件时,抛出异常。默认为0。

返回值:

类型说明
promise<void>promise对象。无返回值。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let srcpath = pathdir + "/source.txt";
let destpath = pathdir + "/dest.txt";
fs.movefile(srcpath, destpath, 0).then(() => {
  console.info("move file succeed");
}).catch((err: businesserror) => {
  console.error("move file failed with error message: " + err.message + ", error code: " + err.code);
});copy to clipboarderrorcopied

fs.movefile

movefile(src: string, dest: string, mode: number, callback: asynccallback<void>): void

移动文件,支持设置移动模式。使用callback异步回调。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
srcstring源文件的应用沙箱路径。
deststring目的文件的应用沙箱路径。
modenumber移动模式。若mode为0,移动位置存在同名文件时,强制移动覆盖。若mode为1,移动位置存在同名文件时,抛出异常。默认为0。
callbackasynccallback<void>异步移动文件之后的回调。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let srcpath = pathdir + "/source.txt";
let destpath = pathdir + "/dest.txt";
fs.movefile(srcpath, destpath, 0, (err: businesserror) => {
  if (err) {
    console.error("move file failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("move file succeed");
  }  
});copy to clipboarderrorcopied

fs.movefile

movefile(src: string, dest: string, callback: asynccallback<void>): void

移动文件,当移动位置存在同名文件时,将强制移动覆盖。使用callback异步回调。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
srcstring源文件的应用沙箱路径。
deststring目的文件的应用沙箱路径。
callbackasynccallback<void>异步移动文件之后的回调。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let srcpath = pathdir + "/source.txt";
let destpath = pathdir + "/dest.txt";
fs.movefile(srcpath, destpath, (err: businesserror) => {
  if (err) {
    console.error("move file failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("move file succeed");
  }  
});copy to clipboarderrorcopied

fs.movefilesync

movefilesync(src: string, dest: string, mode?: number): void

以同步方式移动文件。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
srcstring源文件的应用沙箱路径。
deststring目的文件的应用沙箱路径。
modenumber移动模式。若mode为0,移动位置存在同名文件时,强制移动覆盖。若mode为1,移动位置存在同名文件时,抛出异常。默认为0。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let srcpath = pathdir + "/source.txt";
let destpath = pathdir + "/dest.txt";
fs.movefilesync(srcpath, destpath, 0);
console.info("move file succeed");copy to clipboarderrorcopied

fs.mkdtemp

mkdtemp(prefix: string): promise<string>

创建临时目录,使用promise异步返回。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
prefixstring指定目录路径,命名时需要以"xxxxxx"作为结尾。路径末尾的"xxxxxx"字符串将被替换为随机字符,以创建唯一的目录名。

返回值:

类型说明
promise<string>promise对象。返回生成的唯一目录路径。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
fs.mkdtemp(pathdir + "/xxxxxx").then((dir: string) => {
  console.info("mkdtemp succeed:" + dir);
}).catch((err: businesserror) => {
  console.error("mkdtemp failed with error message: " + err.message + ", error code: " + err.code);
});复制到剪贴板错误复制

fs.mkdtemp

mkdtemp(prefix: string, callback: asynccallback<string>): void

创建临时目录,使用callback异步回调。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
prefix字符串指定目录路径,命名时需要以"xxxxxx"作为结尾。路径末尾的"xxxxxx"字符串将被替换为随机字符,以创建唯一的目录名。
回调asynccallback<string>异步创建临时目录之后的回调。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
fs.mkdtemp(pathdir + "/xxxxxx", (err: businesserror, res: string) => {
  if (err) {
    console.error("mkdtemp failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("mkdtemp succeed");
  }
});复制到剪贴板错误复制

fs.mkdtempsync

mkdtempsync(prefix: string): string

以同步的方法创建临时目录。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
prefix字符串指定目录路径,命名时需要以"xxxxxx"作为结尾。路径末尾的"xxxxxx"字符串将被替换为随机字符,以创建唯一的目录名。

返回值:

类型说明
字符串产生的唯一目录路径。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let res = fs.mkdtempsync(pathdir + "/xxxxxx");复制到剪贴板错误复制

fs.createrandomaccessfile10+

createrandomaccessfile(file: string | file, mode?: number): promise<randomaccessfile>

基于文件路径或文件对象创建randomaccessfile文件对象,使用promise异步返回。

系统能力:systemcapability.filemanagement.file.fileio

参数: | 参数名 | 类型 | 必填 | 说明 | | ------------ | ------ | ------ | ------------------------------------------------------------ | | file | string | file | 是 | 文件的应用沙箱路径或已打开的file对象 | | mode | number | 否 | 创建文件randomaccessfile对象的选项,仅当传入文件沙箱路径时生效,必须指定如下选项中的一个,默认以只读方式创建:
- openmode.read_only(0o0):只读创建。
- openmode.write_only(0o1):只写创建。
- openmode.read_write(0o2):读写创建。
给定如下功能选项,以按位或的方式追加,默认不给定任何额外选项:
- openmode.create(0o100):若文件不存在,则创建文件。
- openmode.trunc(0o1000):如果randomaccessfile对象存在且以只写或读写的方式创建文件,则将其长度裁剪为零。
- openmode.append(0o2000):以追加方式打开,后续写将追加到randomaccessfile对象末尾。
- openmode.nonblock(0o4000):如果path指向fifo、块特殊文件或字符特殊文件,则本次打开及后续 io 进行非阻塞操作。
- openmode.dir(0o200000):如果path不指向目录,则出错。不允许附加写权限。
- openmode.nofollow(0o400000):如果path指向符号链接,则出错。
- openmode.sync(0o4010000):以同步io的方式创建randomaccessfile对象。 |

返回值:

类型说明
promise<randomaccessfile>promise对象。返回randomaccessfile文件对象的结果。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
let file = fs.opensync(filepath, fs.openmode.create | fs.openmode.read_write);
fs.createrandomaccessfile(file).then((randomaccessfile: fs.randomaccessfile) => {
  console.info("randomaccessfile fd: " + randomaccessfile.fd);
  randomaccessfile.close();
}).catch((err: businesserror) => {
  console.error("create randomaccessfile failed with error message: " + err.message + ", error code: " + err.code);
}).finally(() => {
  fs.closesync(file);
});copy to clipboarderrorcopied

fs.createrandomaccessfile10+

createrandomaccessfile(file: string | file, mode: number, callback: asynccallback<randomaccessfile>): void

基于文件路径或文件对象,支持设置创建模式,创建randomaccessfile文件对象。使用callback异步回调。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
filestring | file文件的应用沙箱路径或已打开的file对象
modenumber创建文件randomaccessfile对象的选项,仅当传入文件沙箱路径时生效,必须指定如下选项中的一个,默认以只读方式创建:
- openmode.read_only(0o0):只读创建。
- openmode.write_only(0o1):只写创建。
- openmode.read_write(0o2):读写创建。
给定如下功能选项,以按位或的方式追加,默认不给定任何额外选项:
- openmode.create(0o100):若文件不存在,则创建文件。
- openmode.trunc(0o1000):如果randomaccessfile对象存在且以只写或读写的方式创建文件,则将其长度裁剪为零。
- openmode.append(0o2000):以追加方式打开,后续写将追加到randomaccessfile对象末尾。
- openmode.nonblock(0o4000):如果path指向fifo、块特殊文件或字符特殊文件,则本次打开及后续 io 进行非阻塞操作。
- openmode.dir(0o200000):如果path不指向目录,则出错。不允许附加写权限。
- openmode.nofollow(0o400000):如果path指向符号链接,则出错。
- openmode.sync(0o4010000):以同步io的方式创建randomaccessfile对象。
callbackasynccallback<randomaccessfile>异步创建randomaccessfile对象之后的回调。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
let file = fs.opensync(filepath, fs.openmode.create | fs.openmode.read_write);
fs.createrandomaccessfile(file, fs.openmode.read_write, (err: businesserror, randomaccessfile: fs.randomaccessfile) => {
  if (err) {
    console.error("create randomaccessfile failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("randomaccessfile fd: " + randomaccessfile.fd);
    randomaccessfile.close();
  }
  fs.closesync(file);
});copy to clipboarderrorcopied

fs.createrandomaccessfile10+

createrandomaccessfile(file: string | file, callback: asynccallback<randomaccessfile>): void

基于文件路径或文件对象,以只读方式创建randomaccessfile文件对象。使用callback异步回调。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
filestring | file文件的应用沙箱路径或已打开的file对象
callbackasynccallback<randomaccessfile>异步创建randomaccessfile对象之后的回调。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
let file = fs.opensync(filepath, fs.openmode.create | fs.openmode.read_write);
fs.createrandomaccessfile(file, (err: businesserror, randomaccessfile: fs.randomaccessfile) => {
  if (err) {
    console.error("create randomaccessfile failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("randomaccessfile fd: " + randomaccessfile.fd);
    randomaccessfile.close();
  }
  fs.closesync(file);
});复制到剪贴板错误复制

fs.createrandomaccessfilesync10+

createrandomaccessfilesync(file: string | file, mode?: number): randomaccessfile

基于文件路径或文件对象创建randomaccessfile文件对象。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
filestring|file文件的应用沙箱路径或已打开的file对象
模式创建文件randomaccessfile对象的选项,仅当传入文件沙箱路径时生效,必须指定如下选项中的一个,默认以只读方式创建:
- openmode.read_only(0o0):只读创建。
- openmode.write_only(0o1):只写创建。
- openmode.read_write(0o2):读写创建。
给定如下功能选项,以按位或的方式追加,默认不给定任何额外选项:
- openmode.create(0o100):若文件不存在,则创建文件。
- openmode.trunc(0o1000):如果randomaccessfile对象存在且以只写或读写的方式创建文件,则将其长度裁剪为零。
- openmode.append(0o2000):以追加方式打开,后续写将追加到randomaccessfile对象末尾。
- openmode.nonblock(0o4000):如果path指向fifo、块特殊文件或字符特殊文件,则本次打开及后续 io 进行非阻塞操作。
- openmode.dir(0o200000):如果path不指向目录,则出错。不允许附加写权限。
- openmode.nofollow(0o400000):如果path指向符号链接,则出错。
- openmode.sync(0o4010000):以同步io的方式创建randomaccessfile对象。

返回值:

类型说明
randomaccessfile返回randomaccessfile文件对象。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let filepath = pathdir + "/test.txt";
let file = fs.opensync(filepath, fs.openmode.create | fs.openmode.read_write);
let randomaccessfile = fs.createrandomaccessfilesync(file);
randomaccessfile.close();复制到剪贴板错误复制

fs.createstream

createstream(path: string, mode: string): promise<stream>

基于文件路径创建文件流,使用promise异步返回。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
路径字符串文件的应用沙箱路径。
模式字符串- r:打开只读文件,该文件必须存在。
- r+:打开可读写的文件,该文件必须存在。
- w:打开只写文件,若文件存在则文件长度清0,即该文件内容会消失。若文件不存在则建立该文件。
- w+:打开可读写文件,若文件存在则文件长度清0,即该文件内容会消失。若文件不存在则建立该文件。
- a:以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。
- a+:以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。

返回值:

类型说明
promise<stream>promise对象。返回文件流的结果。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
fs.createstream(filepath, "r+").then((stream: fs.stream) => {
  console.info("createstream succeed");
}).catch((err: businesserror) => {
  console.error("createstream failed with error message: " + err.message + ", error code: " + err.code);
}).finally(() => {
  stream.closesync();
});copy to clipboarderrorcopied

fs.createstream

createstream(path: string, mode: string, callback: asynccallback<stream>): void

基于文件路径创建文件流,使用callback异步回调。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
pathstring文件的应用沙箱路径。
modestring- r:打开只读文件,该文件必须存在。
- r+:打开可读写的文件,该文件必须存在。
- w:打开只写文件,若文件存在则文件长度清0,即该文件内容会消失。若文件不存在则建立该文件。
- w+:打开可读写文件,若文件存在则文件长度清0,即该文件内容会消失。若文件不存在则建立该文件。
- a:以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。
- a+:以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。
callbackasynccallback<stream>异步打开文件流之后的回调。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
fs.createstream(filepath, "r+", (err: businesserror, stream: fs.stream) => {
  if (err) {
    console.error("createstream failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("createstream succeed");
  }
  stream.closesync();
});copy to clipboarderrorcopied

fs.createstreamsync

createstreamsync(path: string, mode: string): stream

以同步方法基于文件路径创建文件流。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
pathstring文件的应用沙箱路径。
modestring- r:打开只读文件,该文件必须存在。
- r+:打开可读写的文件,该文件必须存在。
- w:打开只写文件,若文件存在则文件长度清0,即该文件内容会消失。若文件不存在则建立该文件。
- w+:打开可读写文件,若文件存在则文件长度清0,即该文件内容会消失。若文件不存在则建立该文件。
- a:以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。
- a+:以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。

返回值:

类型说明
stream返回文件流的结果。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let filepath = pathdir + "/test.txt";
let stream = fs.createstreamsync(filepath, "r+");
console.info("createstream succeed");
stream.closesync();copy to clipboarderrorcopied

fs.fdopenstream

fdopenstream(fd: number, mode: string): promise<stream>

基于文件描述符打开文件流,使用promise异步返回。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
fdnumber已打开的文件描述符。
modestring- r:打开只读文件,该文件必须存在。
- r+:打开可读写的文件,该文件必须存在。
- w:打开只写文件,若文件存在则文件长度清0,即该文件内容会消失。若文件不存在则建立该文件。
- w+:打开可读写文件,若文件存在则文件长度清0,即该文件内容会消失。若文件不存在则建立该文件。
- a:以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。
- a+:以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。

返回值:

类型说明
promise<stream>promise对象。返回文件流的结果。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
let file = fs.opensync(filepath);
fs.fdopenstream(file.fd, "r+").then((stream: fs.stream) => {
  console.info("openstream succeed");
  fs.closesync(file);
}).catch((err: businesserror) => {
  console.error("openstream failed with error message: " + err.message + ", error code: " + err.code);
}).finally(() => {
  stream.closesync();
});copy to clipboarderrorcopied

fs.fdopenstream

fdopenstream(fd: number, mode: string, callback: asynccallback<stream>): void

基于文件描述符打开文件流,使用callback异步回调。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
fdnumber已打开的文件描述符。
modestring- r:打开只读文件,该文件必须存在。
- r+:打开可读写的文件,该文件必须存在。
- w:打开只写文件,若文件存在则文件长度清0,即该文件内容会消失。若文件不存在则建立该文件。
- w+:打开可读写文件,若文件存在则文件长度清0,即该文件内容会消失。若文件不存在则建立该文件。
- a:以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。
- a+:以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。
callbackasynccallback<stream>异步打开文件流之后的回调。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
let file = fs.opensync(filepath, fs.openmode.read_only);
fs.fdopenstream(file.fd, "r+", (err: businesserror, stream: fs.stream) => {
  if (err) {
    console.error("fdopen stream failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("fdopen stream succeed");
    fs.closesync(file);
  }
  stream.closesync();
});copy to clipboarderrorcopied

fs.fdopenstreamsync

fdopenstreamsync(fd: number, mode: string): stream

以同步方法基于文件描述符打开文件流。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
fdnumber已打开的文件描述符。
modestring- r:打开只读文件,该文件必须存在。
- r+:打开可读写的文件,该文件必须存在。
- w:打开只写文件,若文件存在则文件长度清0,即该文件内容会消失。若文件不存在则建立该文件。
- w+:打开可读写文件,若文件存在则文件长度清0,即该文件内容会消失。若文件不存在则建立该文件。
- a:以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。
- a+:以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。

返回值:

类型说明
stream返回文件流的结果。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let filepath = pathdir + "/test.txt";
let file = fs.opensync(filepath, fs.openmode.read_only | fs.openmode.create);
let stream = fs.fdopenstreamsync(file.fd, "r+");
fs.closesync(file);
stream.closesync();copy to clipboarderrorcopied

fs.createwatcher10+

createwatcher(path: string, events: number, listener: watcheventlistener): watcher

创建watcher对象,用来监听文件或目录变动。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
pathstring监听文件或目录的沙箱路径。
eventsnumber监听变动的事件集,多个事件通过或(|)的方式进行集合。
- 0x1: in_access, 文件被访问。
- 0x2: in_modify,文件内容被修改。
- 0x4: in_attrib,文件元数据被修改。
- 0x8: in_close_write,文件在打开时进行了写操作,然后被关闭。
- 0x10: in_close_nowrite,文件或目录在打开时未进行写操作,然后被关闭。
- 0x20: in_open,文件或目录被打开。
- 0x40: in_moved_from,监听目录中文件被移动走。
- 0x80: in_moved_to,监听目录中文件被移动过来。
- 0x100: in_create,监听目录中文件或子目录被创建。
- 0x200: in_delete,监听目录中文件或子目录被删除。
- 0x400: in_delete_self,监听的目录被删除,删除后监听停止。
- 0x800: in_move_self,监听的文化或目录被移动,移动后监听继续。
- 0xfff: in_all_events,监听以上所有事件。
listenerwatcheventlistener监听事件发生后的回调。监听事件每发生一次,回调一次。

返回值:

类型说明
watcher返回watcher对象。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import fs, { watchevent } from '@ohos.file.fs';
let filepath = pathdir + "/test.txt";
let file = fs.opensync(filepath, fs.openmode.read_write | fs.openmode.create);
let watcher = fs.createwatcher(filepath, 0x2 | 0x10, (watchevent: watchevent) => {
  if (watchevent.event == 0x2) {
    console.info(watchevent.filename + 'was modified');
  } else if (watchevent.event == 0x10) {
    console.info(watchevent.filename + 'was closed');
  }
});
watcher.start();
fs.writesync(file.fd, 'test');
fs.closesync(file);
watcher.stop();copy to clipboarderrorcopied

watcheventlistener10+

(event: watchevent): void

事件监听类。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
eventwatchevent回调的事件类。

watchevent10+

事件类

系统能力:systemcapability.filemanagement.file.fileio

属性

名称类型可读可写说明
filenamestring发生监听事件的文件名。
eventnumber监听变动的事件集,多个事件通过或(|)的方式进行集合。
- 0x1: in_access, 文件被访问。
- 0x2: in_modify,文件内容被修改。
- 0x4: in_attrib,文件元数据被修改。
- 0x8: in_close_write,文件在打开时进行了写操作,然后被关闭。
- 0x10: in_close_nowrite,文件或目录在打开时未进行写操作,然后被关闭。
- 0x20: in_open,文件或目录被打开。
- 0x40: in_moved_from,监听目录中文件被移动走。
- 0x80: in_moved_to,监听目录中文件被移动过来。
- 0x100: in_create,监听目录中文件或子目录被创建。
- 0x200: in_delete,监听目录中文件或子目录被删除。
- 0x400: in_delete_self,监听的目录被删除,删除后监听停止。
- 0x800: in_move_self,监听的文化或目录被移动,移动后监听继续。
- 0xfff: in_all_events,监听以上所有事件。
cookienumber绑定相关事件的cookie。当前仅支持事件in_moved_from与in_moved_to,同一个文件的移动事件in_moved_from和in_moved_to具有相同的cookie值。

stat

文件具体信息,在调用stat的方法前,需要先通过stat()方法(同步或异步)来构建一个stat实例。

系统能力:systemcapability.filemanagement.file.fileio

属性

名称类型可读可写说明
inobigint标识该文件。通常同设备上的不同文件的ino不同。
modenumber表示文件权限,各特征位的含义如下:
说明: 以下值为八进制,取得的返回值为十进制,请换算后查看。
- 0o400:用户读,对于普通文件,所有者可读取文件;对于目录,所有者可读取目录项。
- 0o200:用户写,对于普通文件,所有者可写入文件;对于目录,所有者可创建/删除目录项。
- 0o100:用户执行,对于普通文件,所有者可执行文件;对于目录,所有者可在目录中搜索给定路径名。
- 0o040:用户组读,对于普通文件,所有用户组可读取文件;对于目录,所有用户组可读取目录项。
- 0o020:用户组写,对于普通文件,所有用户组可写入文件;对于目录,所有用户组可创建/删除目录项。
- 0o010:用户组执行,对于普通文件,所有用户组可执行文件;对于目录,所有用户组是否可在目录中搜索给定路径名。
- 0o004:其他读,对于普通文件,其余用户可读取文件;对于目录,其他用户组可读取目录项。
- 0o002:其他写,对于普通文件,其余用户可写入文件;对于目录,其他用户组可创建/删除目录项。
- 0o001:其他执行,对于普通文件,其余用户可执行文件;对于目录,其他用户组可在目录中搜索给定路径名。
uidnumber文件所有者的id。
gidnumber文件所有组的id。
sizenumber文件的大小,以字节为单位。仅对普通文件有效。
atimenumber上次访问该文件的时间,表示距1970年1月1日0时0分0秒的秒数。
mtimenumber上次修改该文件的时间,表示距1970年1月1日0时0分0秒的秒数。
ctimenumber最近改变文件状态的时间,表示距1970年1月1日0时0分0秒的秒数。

isblockdevice

isblockdevice(): boolean

用于判断文件是否是块特殊文件。一个块特殊文件只能以块为粒度进行访问,且访问的时候带缓存。

系统能力:systemcapability.filemanagement.file.fileio

返回值:

类型说明
boolean表示文件是否是块特殊设备。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let filepath = pathdir + "/test.txt";
let isblockdevice = fs.statsync(filepath).isblockdevice();copy to clipboarderrorcopied

ischaracterdevice

ischaracterdevice(): boolean

用于判断文件是否是字符特殊文件。一个字符特殊设备可进行随机访问,且访问的时候不带缓存。

系统能力:systemcapability.filemanagement.file.fileio

返回值:

类型说明
boolean表示文件是否是字符特殊设备。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let filepath = pathdir + "/test.txt";
let ischaracterdevice = fs.statsync(filepath).ischaracterdevice();copy to clipboarderrorcopied

isdirectory

isdirectory(): boolean

用于判断文件是否是目录。

系统能力:systemcapability.filemanagement.file.fileio

返回值:

类型说明
boolean表示文件是否是目录。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let dirpath = pathdir + "/test";
let isdirectory = fs.statsync(dirpath).isdirectory(); copy to clipboarderrorcopied

isfifo

isfifo(): boolean

用于判断文件是否是命名管道(有时也称为fifo)。命名管道通常用于进程间通信。

系统能力:systemcapability.filemanagement.file.fileio

返回值:

类型说明
boolean表示文件是否是 fifo。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let filepath = pathdir + "/test.txt";
let isfifo = fs.statsync(filepath).isfifo(); copy to clipboarderrorcopied

isfile

isfile(): boolean

用于判断文件是否是普通文件。

系统能力:systemcapability.filemanagement.file.fileio

返回值:

类型说明
boolean表示文件是否是普通文件。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let filepath = pathdir + "/test.txt";
let isfile = fs.statsync(filepath).isfile();copy to clipboarderrorcopied

issocket

issocket(): boolean

用于判断文件是否是套接字。

系统能力:systemcapability.filemanagement.file.fileio

返回值:

类型说明
布尔表示文件是否是套接字。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let filepath = pathdir + "/test.txt";
let issocket = fs.statsync(filepath).issocket(); 复制到剪贴板错误复制

issymboliclink(): boolean

用于判断文件是否是符号链接。

系统能力:systemcapability.filemanagement.file.fileio

返回值:

类型说明
布尔表示文件是否是符号链接。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let filepath = pathdir + "/test";
let issymboliclink = fs.statsync(filepath).issymboliclink(); 复制到剪贴板错误复制

stream

文件流,在调用stream的方法前,需要先通过fs.createstream方法或者fs.fdopenstream(同步或异步)来构建一个stream实例。

close

close(): promise<void>

关闭文件流,使用promise异步返回。

系统能力:systemcapability.filemanagement.file.fileio

返回值:

类型说明
承诺<无效>promise对象。无返回值。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
let stream = fs.createstreamsync(filepath, "r+");
stream.close().then(() => {
  console.info("close filestream succeed");
}).catch((err: businesserror) => {
  console.error("close filestream  failed with error message: " + err.message + ", error code: " + err.code);
});复制到剪贴板错误复制

close

close(callback: asynccallback<void>): void

异步关闭文件流,使用callback异步回调。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
callbackasynccallback<void>异步关闭文件流之后的回调。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
let stream = fs.createstreamsync(filepath, "r+");
stream.close((err: businesserror) => {
  if (err) {
    console.error("close stream failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("close stream succeed");
  }
});copy to clipboarderrorcopied

closesync

closesync(): void

同步关闭文件流。

系统能力:systemcapability.filemanagement.file.fileio

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let filepath = pathdir + "/test.txt";
let stream = fs.createstreamsync(filepath, "r+");
stream.closesync();copy to clipboarderrorcopied

flush

flush(): promise<void>

刷新文件流,使用promise异步返回。

系统能力:systemcapability.filemanagement.file.fileio

返回值:

类型说明
promise<void>promise对象。返回表示异步刷新文件流的结果。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
let stream = fs.createstreamsync(filepath, "r+");
stream.flush().then(() => {
  console.info("flush succeed");
  stream.close();
}).catch((err: businesserror) => {
  console.error("flush failed with error message: " + err.message + ", error code: " + err.code);
});copy to clipboarderrorcopied

flush

flush(callback: asynccallback<void>): void

异步刷新文件流,使用callback异步回调。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
callbackasynccallback<void>异步刷新文件流后的回调函数。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
let stream = fs.createstreamsync(filepath, "r+");
stream.flush((err: businesserror) => {
  if (err) {
    console.error("flush stream failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("flush succeed");
  }
  stream.close();
});copy to clipboarderrorcopied

flushsync

flushsync(): void

同步刷新文件流。

系统能力:systemcapability.filemanagement.file.fileio

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let filepath = pathdir + "/test.txt";
let stream = fs.createstreamsync(filepath, "r+");
stream.flushsync();
stream.close();copy to clipboarderrorcopied

write

write(buffer: arraybuffer | string, options?: { offset?: number; length?: number; encoding?: string; }): promise<number>

将数据写入流文件,使用promise异步返回。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
bufferarraybuffer | string待写入文件的数据,可来自缓冲区或字符串。
optionsobject支持如下选项:
- length,number类型,表示期望写入数据的长度。默认缓冲区长度。
- offset,number类型,表示期望写入文件的位置。可选,默认从当前位置开始写。
- encoding,string类型,当数据是string类型时有效,表示数据的编码方式,默认 'utf-8'。仅支持 'utf-8'。

返回值:

类型说明
promise<number>promise对象。返回实际写入的长度。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
let stream = fs.createstreamsync(filepath, "r+");
class option {
  offset: number = 0;
  length: number = 0;
  encoding: string = 'utf-8';
}
let option = new option();
option.offset = 5;
option.length = 5;
stream.write("hello, world", option).then((number: number) => {
  console.info("write succeed and size is:" + number);
  stream.close();
}).catch((err: businesserror) => {
  console.error("write failed with error message: " + err.message + ", error code: " + err.code);
});copy to clipboarderrorcopied

write

write(buffer: arraybuffer | string, options?: { offset?: number; length?: number; encoding?: string; }, callback: asynccallback<number>): void

将数据写入流文件,使用callback异步回调。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
bufferarraybuffer | string待写入文件的数据,可来自缓冲区或字符串。
optionsobject支持如下选项:
- length,number类型,表示期望写入数据的长度。可选,默认缓冲区长度。
- offset,number类型,表示期望写入文件的位置。可选,默认从当前位置开始写。
- encoding,string类型,当数据是string类型时有效,表示数据的编码方式,默认 'utf-8'。仅支持 'utf-8'。
callbackasynccallback<number>异步写入完成后执行的回调函数。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
let stream = fs.createstreamsync(filepath, "r+");
class option {
  offset: number = 0;
  length: number = 0;
  encoding: string = 'utf-8';
}
let option = new option();
option.offset = 5;
option.length = 5;
stream.write("hello, world", option, (err: businesserror, byteswritten: number) => {
  if (err) {
    console.error("write stream failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    if (byteswritten) {
      console.info("write succeed and size is:" + byteswritten);
      stream.close();
    }
  }
});copy to clipboarderrorcopied

writesync

writesync(buffer: arraybuffer | string, options?: { offset?: number; length?: number; encoding?: string; }): number

以同步方法将数据写入流文件。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
bufferarraybuffer | string待写入文件的数据,可来自缓冲区或字符串。
optionsobject支持如下选项:
- length,number类型,表示期望写入数据的长度。可选,默认缓冲区长度。
- offset,number类型,表示期望写入文件的位置。可选,默认从当前位置开始写。
- encoding,string类型,当数据是string类型时有效,表示数据的编码方式,默认 'utf-8'。仅支持 'utf-8'。

返回值:

类型说明
number实际写入的长度。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let filepath = pathdir + "/test.txt";
let stream = fs.createstreamsync(filepath, "r+");
class option {
  offset: number = 0;
  length: number = 0;
  encoding: string = 'utf-8';
}
let option = new option();
option.offset = 5;
option.length = 5;
let num = stream.writesync("hello, world", option);
stream.close();copy to clipboarderrorcopied

read

read(buffer: arraybuffer, options?: { offset?: number; length?: number; }): promise<number>

从流文件读取数据,使用promise异步返回。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
bufferarraybuffer用于读取文件的缓冲区。
optionsobject支持如下选项:
- length,number类型,表示期望读取数据的长度。可选,默认缓冲区长度。
- offset,number类型,表示期望读取文件的位置。可选,默认从当前位置开始读。

返回值:

类型说明
promise<number>promise对象。返回读取的结果。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
import buffer from '@ohos.buffer';
let filepath = pathdir + "/test.txt";
let stream = fs.createstreamsync(filepath, "r+");
let arraybuffer = new arraybuffer(4096);
class option {
  offset: number = 0;
  length: number = 0;
}
let option = new option();
option.offset = 5;
option.length = 5;
stream.read(arraybuffer, option).then((readlen: number) => {
  console.info("read data succeed");
  let buf = buffer.from(arraybuffer, 0, readlen);
  console.log(`the content of file: ${buf.tostring()}`);
  stream.close();
}).catch((err: businesserror) => {
  console.error("read data failed with error message: " + err.message + ", error code: " + err.code);
});copy to clipboarderrorcopied

read

read(buffer: arraybuffer, options?: { position?: number; offset?: number; length?: number; }, callback: asynccallback<number>): void

从流文件读取数据,使用callback异步回调。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
bufferarraybuffer用于读取文件的缓冲区。
optionsobject支持如下选项:
- length,number类型,表示期望读取数据的长度。可选,默认缓冲区长度。
- offset,number类型,表示期望读取文件的位置。可选,默认从当前位置开始读.
callbackasynccallback<number>异步从流文件读取数据之后的回调。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
import buffer from '@ohos.buffer';
let filepath = pathdir + "/test.txt";
let stream = fs.createstreamsync(filepath, "r+");
let arraybuffer = new arraybuffer(4096);
class option {
  offset: number = 0;
  length: number = 0;
}
let option = new option();
option.offset = 5;
option.length = 5;
stream.read(arraybuffer, option, (err: businesserror, readlen: number) => {
  if (err) {
    console.error("read stream failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("read data succeed");
    let buf = buffer.from(arraybuffer, 0, readlen);
    console.log(`the content of file: ${buf.tostring()}`);
  }
  stream.close();
});copy to clipboarderrorcopied

readsync

readsync(buffer: arraybuffer, options?: { offset?: number; length?: number; }): number

以同步方法从流文件读取数据。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
bufferarraybuffer用于读取文件的缓冲区。
optionsobject支持如下选项:
- length,number类型,表示期望读取数据的长度。可选,默认缓冲区长度。
- offset,number类型,表示期望读取文件的位置。可选,默认从当前位置开始读。

返回值:

类型说明
number实际读取的长度。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let filepath = pathdir + "/test.txt";
let stream = fs.createstreamsync(filepath, "r+");
class option {
  offset: number = 0;
  length: number = 0;
}
let option = new option();
option.offset = 5;
option.length = 5;
let buf = new arraybuffer(4096);
let num = stream.readsync(buf, option);
stream.close();copy to clipboarderrorcopied

file

由open接口打开的file对象。

系统能力:systemcapability.filemanagement.file.fileio

属性

名称类型可读可写说明
fdnumber打开的文件描述符。
path10+string文件路径。
name10+string文件名。

lock

lock(exclusive?: boolean): promise<void>

文件阻塞式施加共享锁或独占锁,使用promise异步返回。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
exclusiveboolean是否施加独占锁,默认false。

返回值:

类型说明
promise<void>promise对象。无返回值。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
let file = fs.opensync(filepath, fs.openmode.read_write | fs.openmode.create);
file.lock(true).then(() => {
  console.log("lock file succeed");
}).catch((err: businesserror) => {
  console.error("lock file failed with error message: " + err.message + ", error code: " + err.code);
}).finally(() => {
  fs.closesync(file);
});copy to clipboarderrorcopied

lock

lock(exclusive?: boolean, callback: asynccallback<void>): void

文件阻塞式施加共享锁或独占锁,使callback异步回调。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
exclusiveboolean是否施加独占锁,默认false。
callbackasynccallback<void>异步文件上锁之后的回调。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
let file = fs.opensync(filepath, fs.openmode.read_write | fs.openmode.create);
file.lock(true, (err: businesserror) => {
  if (err) {
    console.error("lock file failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.log("lock file succeed");
  }
  fs.closesync(file);
});copy to clipboarderrorcopied

trylock

trylock(exclusive?: boolean): void

文件非阻塞式施加共享锁或独占锁。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
exclusiveboolean是否施加独占锁,默认false。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let filepath = pathdir + "/test.txt";
let file = fs.opensync(filepath, fs.openmode.read_write | fs.openmode.create);
file.trylock(true);
console.log("lock file succeed");
fs.closesync(file);copy to clipboarderrorcopied

unlock

unlock(): void

以同步方式给文件解锁。

系统能力:systemcapability.filemanagement.file.fileio

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let filepath = pathdir + "/test.txt";
let file = fs.opensync(filepath, fs.openmode.read_write | fs.openmode.create);
file.trylock(true);
file.unlock();
console.log("unlock file succeed");
fs.closesync(file);copy to clipboarderrorcopied

randomaccessfile

随机读写文件流,在调用randomaccessfile的方法前,需要先通过createrandomaccess()方法(同步或异步)来构建一个randomaccessfile实例。

系统能力:systemcapability.filemanagement.file.fileio

属性

名称类型可读可写说明
fdnumber打开的文件描述符。
filepointernumberrandomaccessfile对象的偏置指针。

setfilepointer10+

setfilepointer(): void

设置文件偏置指针

系统能力:systemcapability.filemanagement.file.fileio

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let filepath = pathdir + "/test.txt";
let randomaccessfile = fs.createrandomaccessfilesync(filepath, fs.openmode.read_write | fs.openmode.create);
randomaccessfile.setfilepointer(1);
randomaccessfile.close();copy to clipboarderrorcopied

close10+

close(): void

同步关闭randomaccessfile对象。

系统能力:systemcapability.filemanagement.file.fileio

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let filepath = pathdir + "/test.txt";
let randomaccessfile = fs.createrandomaccessfilesync(filepath, fs.openmode.read_write | fs.openmode.create);
randomaccessfile.close();copy to clipboarderrorcopied

write10+

write(buffer: arraybuffer | string, options?: { offset?: number; length?: number; encoding?: string; }): promise<number>

将数据写入文件,使用promise异步返回。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
bufferarraybuffer | string待写入文件的数据,可来自缓冲区或字符串。
optionsobject支持如下选项:
- length,number类型,表示期望写入数据的长度。默认缓冲区长度。
- offset,number类型,表示期望写入文件位置(基于当前filepointer加上offset的位置)。可选,默认从偏置指针(filepointer)开始写。
- encoding,string类型,当数据是string类型时有效,表示数据的编码方式,默认 'utf-8'。仅支持 'utf-8'。

返回值:

类型说明
promise<number>promise对象。返回实际写入的长度。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
let file = fs.opensync(filepath, fs.openmode.create | fs.openmode.read_write);
let randomaccessfile = fs.createrandomaccessfilesync(file);
let bufferlength: number = 4096;
class option {
  offset: number = 0;
  length: number = 0;
  encoding: string = 'utf-8';
}
let option = new option();
option.offset = 1;
option.length = 5;
let arraybuffer = new arraybuffer(bufferlength);
randomaccessfile.write(arraybuffer, option).then((byteswritten: number) => {
  console.info("randomaccessfile byteswritten: " + byteswritten);
}).catch((err: businesserror) => {
  console.error("create randomaccessfile failed with error message: " + err.message + ", error code: " + err.code);
}).finally(() => {
  randomaccessfile.close();
  fs.closesync(file);
});
copy to clipboarderrorcopied

write10+

write(buffer: arraybuffer | string, options?: { offset?: number; length?: number; encoding?: string; }, callback: asynccallback<number>): void

将数据写入文件,使用callback异步回调。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
bufferarraybuffer | string待写入文件的数据,可来自缓冲区或字符串。
optionsobject支持如下选项:
- length,number类型,表示期望写入数据的长度。可选,默认缓冲区长度。
- offset,number类型,表示期望写入文件位置(基于当前filepointer加上offset的位置)。可选,默认从偏置指针(filepointer)开始写。
- encoding,string类型,当数据是string类型时有效,表示数据的编码方式,默认 'utf-8'。仅支持 'utf-8'。
callbackasynccallback<number>异步写入完成后执行的回调函数。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
let file = fs.opensync(filepath, fs.openmode.create | fs.openmode.read_write);
let randomaccessfile = fs.createrandomaccessfilesync(file);
let bufferlength: number = 4096;
class option {
  offset: number = 0;
  length: number = bufferlength;
  encoding: string = 'utf-8';
}
let option = new option();
option.offset = 1;
let arraybuffer = new arraybuffer(bufferlength);
randomaccessfile.write(arraybuffer, option, (err: businesserror, byteswritten: number) => {
  if (err) {
    console.error("write failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    if (byteswritten) {
      console.info("write succeed and size is:" + byteswritten);
    }
  }
  randomaccessfile.close();
  fs.closesync(file);
});copy to clipboarderrorcopied

writesync10+

writesync(buffer: arraybuffer | string, options?: { offset?: number; length?: number; encoding?: string; }): number

以同步方法将数据写入文件。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
bufferarraybuffer | string待写入文件的数据,可来自缓冲区或字符串。
optionsobject支持如下选项:
- length,number类型,表示期望写入数据的长度。可选,默认缓冲区长度。
- offset,number类型,表示期望写入文件位置(基于当前filepointer加上offset的位置)。可选,默认从偏置指针(filepointer)开始写。
- encoding,string类型,当数据是string类型时有效,表示数据的编码方式,默认 'utf-8'。仅支持 'utf-8'。

返回值:

类型说明
number实际写入的长度。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let filepath = pathdir + "/test.txt";
let randomaccessfile = fs.createrandomaccessfilesync(filepath, fs.openmode.create | fs.openmode.read_write);
class option {
  offset: number = 0;
  length: number = 0;
  encoding: string = 'utf-8';
}
let option = new option();
option.offset = 5;
option.length = 5;
let byteswritten = randomaccessfile.writesync("hello, world", option);
randomaccessfile.close();copy to clipboarderrorcopied

read10+

read(buffer: arraybuffer, options?: { offset?: number; length?: number; }): promise<number>

从文件读取数据,使用promise异步返回。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
bufferarraybuffer用于读取文件的缓冲区。
optionsobject支持如下选项:
- length,number类型,表示期望读取数据的长度。可选,默认缓冲区长度。
- offset,number类型,表示期望读取文件位置(基于当前filepointer加上offset的位置)。可选,默认从偏置指针(filepointer)开始读。

返回值:

类型说明
promise<number>promise对象。返回读取的结果。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
let file = fs.opensync(filepath, fs.openmode.create | fs.openmode.read_write);
let randomaccessfile = fs.createrandomaccessfilesync(file);
let bufferlength: number = 4096;
class option {
  offset: number = 0;
  length: number = bufferlength;
}
let option = new option();
option.offset = 1;
option.length = 5;
let arraybuffer = new arraybuffer(bufferlength);
randomaccessfile.read(arraybuffer, option).then((readlength: number) => {
  console.info("randomaccessfile readlength: " + readlength);
}).catch((err: businesserror) => {
  console.error("create randomaccessfile failed with error message: " + err.message + ", error code: " + err.code);
}).finally(() => {
  randomaccessfile.close();
  fs.closesync(file);
});copy to clipboarderrorcopied

read10+

read(buffer: arraybuffer, options?: { position?: number; offset?: number; length?: number; }, callback: asynccallback<number>): void

从文件读取数据,使用callback异步回调。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
bufferarraybuffer用于读取文件的缓冲区。
optionsobject支持如下选项:
- length,number类型,表示期望读取数据的长度。可选,默认缓冲区长度。
- offset,number类型,表示期望读取文件位置(基于当前filepointer加上offset的位置)。可选,默认从偏置指针(filepointer)开始读.
callbackasynccallback<number>异步从流文件读取数据之后的回调。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

import { businesserror } from '@ohos.base';
let filepath = pathdir + "/test.txt";
let file = fs.opensync(filepath, fs.openmode.create | fs.openmode.read_write);
let randomaccessfile = fs.createrandomaccessfilesync(file);
let length: number = 20;
class option {
  offset: number = 0;
  length: number = length;
}
let option = new option();
option.offset = 1;
option.length = 5;
let arraybuffer = new arraybuffer(length);
randomaccessfile.read(arraybuffer, option, (err: businesserror, readlength: number) => {
  if (err) {
    console.error("read failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    if (readlength) {
      console.info("read succeed and size is:" + readlength);
    }
  }
  randomaccessfile.close();
  fs.closesync(file);
});copy to clipboarderrorcopied

readsync10+

readsync(buffer: arraybuffer, options?: { offset?: number; length?: number; }): number

以同步方法从文件读取数据。

系统能力:systemcapability.filemanagement.file.fileio

参数:

参数名类型必填说明
bufferarraybuffer用于读取文件的缓冲区。
optionsobject支持如下选项:
- length,number类型,表示期望读取数据的长度。可选,默认缓冲区长度。
- offset,number类型,表示期望读取文文件位置(基于当前filepointer加上offset的位置)。可选,默认从偏置指针(filepointer)开始读。

返回值:

类型说明
number实际读取的长度。

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let filepath = pathdir + "/test.txt";
let file = fs.opensync(filepath, fs.openmode.create | fs.openmode.read_write);
let randomaccessfile = fs.createrandomaccessfilesync(file);
let length: number = 4096;
let arraybuffer = new arraybuffer(length);
let readlength = randomaccessfile.readsync(arraybuffer);
randomaccessfile.close();
fs.closesync(file);copy to clipboarderrorcopied

watcher10+

文件目录变化监听对象。由createwatcher接口获得。

start10+

start(): void

开启监听。

系统能力:systemcapability.filemanagement.file.fileio

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let filepath = pathdir + "/test.txt";
let watcher = fs.createwatcher(filepath, 0xfff, () => {});
watcher.start();
watcher.stop();copy to clipboarderrorcopied

stop10+

stop(): void

停止监听。

系统能力:systemcapability.filemanagement.file.fileio

错误码:

接口抛出错误码的详细介绍请参见基础文件io错误码

示例:

let filepath = pathdir + "/test.txt";
let watcher = fs.createwatcher(filepath, 0xfff, () => {});
watcher.start();
watcher.stop();copy to clipboarderrorcopied

openmode

open接口flags参数常量。文件打开标签。

系统能力:systemcapability.filemanagement.file.fileio

名称类型说明
read_onlynumber0o0只读打开。
write_onlynumber0o1只写打开。
read_writenumber0o2读写打开。
createnumber0o100若文件不存在,则创建文件。
truncnumber0o1000如果文件存在且以只写或读写的方式打开文件,则将其长度裁剪为零。
appendnumber0o2000以追加方式打开,后续写将追加到文件末尾。
nonblocknumber0o4000如果path指向fifo、块特殊文件或字符特殊文件,则本次打开及后续 io 进行非阻塞操作。
dirnumber0o200000如果path不指向目录,则出错。
nofollownumber0o400000如果path指向符号链接,则出错。
syncnumber0o4010000以同步io的方式打开文件。

filter10+

系统能力:systemcapability.filemanagement.file.fileio

文件过滤配置项类型,支持listfile接口使用。

名称类型说明
suffix数组<字符串>文件后缀名完全匹配,各个关键词or关系。
displayname数组<字符串>文件名模糊匹配,各个关键词or关系。当前仅支持通配符*。
mimetype数组<字符串>mime类型完全匹配,各个关键词or关系。
filesizeover文件大小匹配,大于等于指定大小的文件。
lastmodifiedafter文件最近修改时间匹配,在指定时间点及之后的文件。
excludemedia布尔是否排除media中已有的文件。

conflictfiles10+

系统能力:systemcapability.filemanagement.file.fileio

冲突文件信息,支持copydir及movedir接口使用。

名称类型说明
srcfile字符串源冲突文件路径。
destfile字符串目标冲突文件路径。

最后

有很多小伙伴不知道学习哪些鸿蒙开发技术?不知道需要重点掌握哪些鸿蒙应用开发知识点?而且学习时频繁踩坑,最终浪费大量时间。所以有一份实用的鸿蒙(harmonyos next)资料用来跟着学习是非常有必要的。 

这份鸿蒙(harmonyos next)资料包含了鸿蒙开发必掌握的核心知识要点,内容包含了arkts、arkui开发组件、stage模型、多端部署、分布式应用开发、音频、视频、webgl、openharmony多媒体技术、napi组件、openharmony内核、harmony南向开发、鸿蒙项目实战等等)鸿蒙(harmonyos next)技术知识点。

希望这一份鸿蒙学习资料能够给大家带来帮助,有需要的小伙伴自行领取,限时开源,先到先得~无套路领取!!

获取这份完整版高清学习路线,请点击→纯血版全套鸿蒙harmonyos学习资料

鸿蒙(harmonyos next)最新学习路线

  •  harmonos基础技能

  • harmonos就业必备技能 
  •  harmonos多媒体技术

  • 鸿蒙napi组件进阶

  • harmonos高级技能

  • 初识harmonos内核 
  • 实战就业级设备开发

有了路线图,怎么能没有学习资料呢,小编也准备了一份联合鸿蒙官方发布笔记整理收纳的一套系统性的鸿蒙(openharmony )学习手册(共计1236页)鸿蒙(openharmony )开发入门教学视频,内容包含:arkts、arkui、web开发、应用模型、资源分类…等知识点。

获取以上完整版高清学习路线,请点击→纯血版全套鸿蒙harmonyos学习资料

《鸿蒙 (openharmony)开发入门教学视频》

《鸿蒙生态应用开发v2.0白皮书》

图片

《鸿蒙 (openharmony)开发基础到实战手册》

openharmony北向、南向开发环境搭建

图片

 《鸿蒙开发基础》

  • arkts语言
  • 安装deveco studio
  • 运用你的第一个arkts应用
  • arkui声明式ui开发
  • .……

图片

 《鸿蒙开发进阶》

  • stage模型入门
  • 网络管理
  • 数据管理
  • 电话服务
  • 分布式应用开发
  • 通知与窗口管理
  • 多媒体技术
  • 安全技能
  • 任务管理
  • webgl
  • 国际化开发
  • 应用测试
  • dfx面向未来设计
  • 鸿蒙系统移植和裁剪定制
  • ……

图片

《鸿蒙进阶实战》

  • arkts实践
  • uiability应用
  • 网络案例
  • ……

图片

 获取以上完整鸿蒙harmonyos学习资料,请点击→纯血版全套鸿蒙harmonyos学习资料

总结

总的来说,华为鸿蒙不再兼容安卓,对中年程序员来说是一个挑战,也是一个机会。只有积极应对变化,不断学习和提升自己,他们才能在这个变革的时代中立于不败之地。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com