HarmonyOS NEXT鸿蒙开发 ArkTS:@ohos.file.fs文件打开、关闭、是否存在、查看文件信息操作 作者:马育民 • 2025-12-11 20:41 • 阅读:10003 # 介绍 [官网API](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-file-fs "官网API") 本文介绍 `ohos.file.fs` 对文件进行操作 # 导包 ``` import { fileIo as fs } from '@kit.CoreFileKit'; import { BusinessError } from '@kit.BasicServicesKit'; ``` # 错误码 下面很多操作中,会抛出异常,异常信息中包含错误码,详见 [链接](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/errorcode-filemanagement#%E5%9F%BA%E7%A1%80%E6%96%87%E4%BB%B6io%E9%94%99%E8%AF%AF%E7%A0%81 "链接") # 打开、创建文件 ### 同步方式 ``` openSync(path: string, mode?: number): File ``` **解释:** - path:`string` 类型,必填,打开文件或目录的应用沙箱路径或URI。 - mode:`number` 类型,打开文件或目录的选项。默认以只读方式打开 - OpenMode.READ_ONLY(0o0):只读打开。 - OpenMode.WRITE_ONLY(0o1):只写打开。 - OpenMode.READ_WRITE(0o2):读写打开。 - 给定如下功能选项,以按 **位或** 的方式追加,默认不给定任何额外选项: - OpenMode.CREATE(0o100):若文件不存在,则创建文件。 - OpenMode.TRUNC(0o1000):如果文件存在且文件具有写权限,则将其长度裁剪为零。 - OpenMode.APPEND(0o2000):以追加方式打开,后续写将追加到文件末尾。 - OpenMode.DIR(0o200000):如果path不指向目录,则出错。不允许附加写权限。 - OpenMode.SYNC(0o4010000):以同步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); ``` ### 异步方式 ``` open(path: string, mode?: number): Promise ``` **参数:**如上 **返回值:** `Promise`:`Promise` 对象。返回 `File` 对象。 ##### 例子 ``` import { BusinessError } from '@kit.BasicServicesKit'; 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); }); ``` # 关闭文件 以同步方法关闭文件或目录。 **提示:**打开文件后,要及时的关闭 ``` closeSync(file: number | File): void ``` **参数:** - file:`number | File`,必填,已打开的 `File` 对象或已打开的文件描述符 `fd` 。关闭后 `file` 对象或文件描述符 `fd` 不再具备实际意义,不可再用于进行读写等操作。 ##### 例子 ``` let filePath = pathDir + "/test.txt"; let file = fs.openSync(filePath); fs.closeSync(file); ``` # 获取详细信息 获取文件或目录详细属性信息,使用promise异步回调。 ### 同步方式 以同步方法获取文件或目录详细属性信息。 ``` statSync(file: string | number): Stat ``` **参数:** - file:`string | number`,必填,文件或目录应用沙箱路径path或已打开的文件描述符fd。 **返回值:** `Stat`类型,表示文件或目录的具体信息。 ###### 例子 ``` let stat = fs.statSync(pathDir); console.info("get file info succeed, the size of file is " + stat.size); ``` ### 异步方式 ``` stat(file: string | number): Promise ``` **参数:** - file:`string | number`,必填,文件或目录应用沙箱路径path或已打开的文件描述符fd。 **返回值:**`Promise`:`Promise`对象。返回文件或目录的具体信息。 ##### 例子 ``` 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); }); ``` ### Stat 文件具体信息,在调用Stat的方法前,需要先通过stat()方法(同步或异步)构建一个Stat实例。 ##### 属性 - ino:`bigint` 类型,标识该文件。通常同设备上的不同文件的INO不同。 - mode:`number`,表示文件权限,各特征位的含义如下(说明:以下值为八进制,取得的返回值为十进制,请换算后查看。): - 0o400:用户读。对于普通文件,所有者可读取文件;对于目录,所有者可读取目录项。 - 0o200:用户写。对于普通文件,所有者可写入文件;对于目录,所有者可创建/删除目录项。 - 0o100:用户执行。对于普通文件,所有者可执行文件;对于目录,所有者可在目录中搜索给定路径名。 - 0o040:用户组读。对于普通文件,所有用户组可读取文件;对于目录,所有用户组可读取目录项。 - 0o020:用户组写。对于普通文件,所有用户组可写入文件;对于目录,所有用户组可创建/删除目录项。 - 0o010:用户组执行。对于普通文件,所有用户组可执行文件;对于目录,所有用户组是否可在目录中搜索给定路径名。 - 0o004:其他读。对于普通文件,其余用户可读取文件;对于目录,其他用户组可读取目录项。 - 0o002:其他写。对于普通文件,其余用户可写入文件;对于目录,其他用户组可创建/删除目录项。 - 0o001:其他执行。对于普通文件,其余用户可执行文件;对于目录,其他用户组可在目录中搜索给定路径名。 - uid:`number` 类型,文件所有者的ID。 - gid:`number` 类型,文件所有组的ID。 - size:`number` 类型,文件的大小,以字节为单位。仅对普通文件有效。 - atime:`number` 类型,上次访问该文件的时间,表示距1970年1月1日0时0分0秒的秒数。注意:目前用户数据分区默认以“noatime”方式挂载,atime更新被禁用。 - mtime:`number` 类型,上次修改该文件的时间,表示距1970年1月1日0时0分0秒的秒数。 - ctime:`number` 类型,最近改变文件状态的时间,表示距1970年1月1日0时0分0秒的秒数。 - atimeNs:`bigint` 类型,上次访问该文件的时间,表示距1970年1月1日0时0分0秒的纳秒数。注意:目前用户数据分区默认以“noatime”方式挂载,atime更新被禁用。 - mtimeNs:`bigint` 类型,上次修改该文件的时间,表示距1970年1月1日0时0分0秒的纳秒数。 - ctimeNs:`bigint` 类型,最近改变文件状态的时间,表示距1970年1月1日0时0分0秒的纳秒数。 - location:`LocationType` 类型,文件的位置,表示该文件是本地文件或者云端文件。 # 文件是否存在 ### 同步方式 ``` accessSync(path: string, mode?: AccessModeType): boolean ``` **参数:** - path:`string` 类型,必填,文件或目录应用沙箱路径。 - mode:`AccessModeType` 类型,文件或目录校验的权限。不填该参数则默认校验文件或目录是否存在。 **返回值:** `boolean` 类型,返回true,表示文件存在;返回false,表示文件不存在。 ##### 例子 ``` import { BusinessError } from '@kit.BasicServicesKit'; 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); } ``` ### 异步方式 ``` access(path: string, mode?: AccessModeType): Promise ``` **参数:**同上 **返回值:**`Promise`,`Promise`对象。返回布尔值。返回true,表示文件存在;返回false,表示文件不存在。 ##### 例子 ``` import { BusinessError } from '@kit.BasicServicesKit'; 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); }); ``` 原文出处:http://www.malaoshi.top/show_1GW2OQZoRrAv.html