# uni.getFileSystemManager()

仅 HarmonyOS 支持

获取文件管理器

# getFileSystemManager 兼容性

HarmonyOS
HBuilderX 4.45

# 返回值

类型
FileSystemManager

# FileSystemManager 的方法

# readFile(options: ReadFileOptions): void;

读取本地文件内容

# readFile 兼容性
HarmonyOS
HBuilderX 4.51
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
options ReadFileOptions - - -
名称 类型 必填 默认值 兼容性 描述
encoding string - - base64 / utf-8 / ascii,指定读取文件的字符编码,(iOS平台4.61及以后、Android平台4.31及以后)如果不传 encoding,则以 ArrayBuffer 格式读取文件的二进制内容
filePath string.URIString - - 文件路径,支持相对地址和绝对地址,app-android平台支持代码包文件目录
success (res: ReadFileSuccessResult) => void - - 接口调用的回调函数
fail (res: IFileSystemManagerFail) => void - - 通用的错误返回结果回调
complete (res: any) => void - - 通用的结束返回结果回调
# ReadFileSuccessResult 的属性值
名称 类型 必填 默认值 兼容性 描述
data string | ArrayBuffer -
读取的内容,类型为 String 或 ArrayBuffer,在4.31以前类型是string,Android平台4.31、iOS平台4.61起支持ArrayBuffer
# IFileSystemManagerFail 的属性值
名称 类型 必填 默认值 兼容性 描述
errCode number - - 错误码
errSubject string - - 统一错误主题(模块)名称
data any - - 错误信息中包含的数据
cause Error - - 源错误信息,可以包含多个错误,详见SourceError
errMsg string - - -

# readFileSync(filePath: string, encoding?: string): string | ArrayBuffer;

FileSystemManager.readFile 的同步版本参数

# readFileSync 兼容性
HarmonyOS
HBuilderX 4.51
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
filePath string - - 文件路径,支持相对地址和绝对地址,app-android平台支持代码包文件目录
encoding string - - base64 / utf-8,指定读取文件的字符编码,(iOS平台4.61及以后、Android平台4.31及以后)如果不传 encoding,则以 ArrayBuffer 格式读取文件的二进制内容
# 返回值
类型
string | ArrayBuffer

# writeFile(options: WriteFileOptions): void;

写文件

# writeFile 兼容性
HarmonyOS
HBuilderX 4.41
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
options WriteFileOptions - - -
名称 类型 必填 默认值 兼容性 描述
filePath string.URIString - - 文件路径,只支持绝对地址
encoding string - - 指定写入文件的字符编码,
支持:ascii base64 utf-8,默认值是 utf-8,仅在 data 类型是 String 时有效
data string | ArrayBuffer -
写入的内容,类型为 String 或 ArrayBuffer,之前类型是string,iOS平台4.61及以后、Android平台4.31及以后支持ArrayBuffer类型
success (res: FileManagerSuccessResult) => void - - 通用的正确返回结果回调
fail (res: IFileSystemManagerFail) => void - - 接口调用失败的回调函数
complete (res: any) => void - - 接口调用结束的回调函数(调用成功、失败都会执行)

# read(option: ReadOption): void;

读文件

# read 兼容性
HarmonyOS
HBuilderX 4.51
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
option ReadOption - - -
名称 类型 必填 默认值 兼容性 描述
arrayBuffer ArrayBuffer - - 数据写入的缓冲区,必须是 ArrayBuffer 实例
fd string - - 文件描述符。fd 通过 FileSystemManager.open 或 FileSystemManager.openSync 接口获得
length number - - 要从文件中读取的字节数,默认0
offset number - - 缓冲区中的写入偏移量,默认0
position number - - 文件读取的起始位置,如不传或传 null,则会从当前文件指针的位置读取。如果 position 是正整数,则文件指针位置会保持不变并从 position 读取文件。
complete (res: any) => void - - 接口调用结束的回调函数(调用成功、失败都会执行)
fail (res: IFileSystemManagerFail) => void - - 接口调用失败的回调函数
success (result: ReadSuccessCallbackResult) => void - - 接口调用成功的回调函数
# ReadSuccessCallbackResult 的属性值
名称 类型 必填 默认值 兼容性 描述
arrayBuffer ArrayBuffer -
被写入的缓存区的对象,即接口入参的 arrayBuffer
bytesRead number -
实际读取的字节数

# readSync(option: ReadSyncOption): ReadResult;

读文件

# readSync 兼容性
HarmonyOS
HBuilderX 4.51
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
option ReadSyncOption - - -
名称 类型 必填 默认值 兼容性 描述
arrayBuffer ArrayBuffer -
数据写入的缓冲区,必须是 ArrayBuffer 实例
fd string - - 文件描述符。fd 通过 FileSystemManager.openFileSystemManager.openSync 接口获得
length number - - 要从文件中读取的字节数,默认0
offset number - - 缓冲区中的写入偏移量,默认0
position number - - 文件读取的起始位置,如不传或传 null,则会从当前文件指针的位置读取。如果 position 是正整数,则文件指针位置会保持不变并从 position 读取文件。
# 返回值
类型
ReadResult
名称 类型 必填 默认值 兼容性 描述
arrayBuffer ArrayBuffer -
被写入的缓存区的对象,即接口入参的 arrayBuffer
bytesRead number -
实际读取的字节数

# writeFileSync(filePath: string, data: string | ArrayBuffer, encoding?: string): void;

FileSystemManager.writeFile 的同步版本

# writeFileSync 兼容性
HarmonyOS
HBuilderX 4.41
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
filePath string - - 文件路径,只支持绝对地址
data string | ArrayBuffer - - 要写入的文本或二进制数据,Android平台4.31、iOS平台4.61及以后版本支持ArrayBuffer
encoding string - - 指定写入文件的字符编码,支持:ascii base64 utf-8, 默认值是utf-8, 仅在 data 类型是 String 时有效

删除文件

HarmonyOS
HBuilderX 4.51
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
options UnLinkOptions - - -
名称 类型 必填 默认值 兼容性 描述
filePath string.URIString - - 文件路径,只支持绝对地址
success (res: FileManagerSuccessResult) => void - - 接口调用的回调函数
fail (res: IFileSystemManagerFail) => void - - 接口调用失败的回调函数
complete (res: any) => void - - 接口调用结束的回调函数(调用成功、失败都会执行)

# unlinkSync(filePath: string): void;

FileSystemManager.unlink 的同步版本

# unlinkSync 兼容性
HarmonyOS
HBuilderX 4.51
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
filePath string - - 文件路径,只支持绝对地址

# mkdir(options: MkDirOptions): void;

创建目录

# mkdir 兼容性
HarmonyOS
HBuilderX 4.41
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
options MkDirOptions - - -
名称 类型 必填 默认值 兼容性 描述
dirPath string.URIString - - 创建的目录路径 (本地路径)
recursive boolean - - 是否在递归创建该目录的上级目录后再创建该目录。如果对应的上级目录已经存在,则不创建该上级目录。如 dirPath 为 a/b/c/d 且 recursive 为 true,将创建 a 目录,再在 a 目录下创建 b 目录,以此类推直至创建 a/b/c 目录下的 d 目录。
success (res: FileManagerSuccessResult) => void - - 接口调用的回调函数
fail (res: IFileSystemManagerFail) => void - - 接口调用失败的回调函数
complete (res: any) => void - - 接口调用结束的回调函数(调用成功、失败都会执行)

# mkdirSync(dirPath: string, recursive: boolean): void;

FileSystemManager.mkdir 的同步版本

# mkdirSync 兼容性
HarmonyOS
HBuilderX 4.41
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
dirPath string - - 创建的目录路径 (本地路径)
recursive boolean - - 是否在递归创建该目录的上级目录后再创建该目录。如果对应的上级目录已经存在,则不创建该上级目录。如 dirPath 为 a/b/c/d 且 recursive 为 true,将创建 a 目录,再在 a 目录下创建 b 目录,以此类推直至创建 a/b/c 目录下的 d 目录。

# rmdir(options: RmDirOptions): void;

删除目录

# rmdir 兼容性
HarmonyOS
HBuilderX 4.51
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
options RmDirOptions - - -
名称 类型 必填 默认值 兼容性 描述
dirPath string.URIString - - 要删除的目录路径 (本地路径)
recursive boolean - - 是否递归删除目录。如果为 true,则删除该目录和该目录下的所有子目录以及文件。
success (res: FileManagerSuccessResult) => void - - 接口调用的回调函数
fail (res: IFileSystemManagerFail) => void - - 接口调用失败的回调函数
complete (res: any) => void - - 接口调用结束的回调函数(调用成功、失败都会执行)

# rmdirSync(dirPath: string, recursive: boolean): void;

FileSystemManager.rmdir 的同步版本

# rmdirSync 兼容性
HarmonyOS
HBuilderX 4.51
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
dirPath string - - 要删除的目录路径 (本地路径)
recursive boolean - - 是否递归删除目录。如果为 true,则删除该目录和该目录下的所有子目录以及文件。

# readdir(options: ReadDirOptions): void;

读取目录内文件列表

# readdir 兼容性
HarmonyOS
HBuilderX 4.51
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
options ReadDirOptions - - -
名称 类型 必填 默认值 兼容性 描述
dirPath string.URIString - - 要读取的目录路径 (本地路径)
success (res: ReadDirSuccessResult) => void - - 接口调用的回调函数
fail (res: IFileSystemManagerFail) => void - - 接口调用失败的回调函数
complete (res: any) => void - - 接口调用结束的回调函数(调用成功、失败都会执行)
# ReadDirSuccessResult 的属性值
名称 类型 必填 默认值 兼容性 描述
files Array<string> - - -

# readdirSync(dirPath: string): string[] | null;

FileSystemManager.readdir 的同步版本

# readdirSync 兼容性
HarmonyOS
HBuilderX 4.51
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
dirPath string - - 要读取的目录路径 (本地路径)
# 返回值
类型 必备
Array<string>

# access(options: AccessOptions): void;

判断文件/目录是否存在

# access 兼容性
HarmonyOS
HBuilderX 4.51
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
options AccessOptions - - -
名称 类型 必填 默认值 兼容性 描述
path string.URIString - - 要判断是否存在的文件/目录路径 (本地路径)
success (res: FileManagerSuccessResult) => void - - 接口调用的回调函数
fail (res: IFileSystemManagerFail) => void - - 接口调用失败的回调函数
complete (res: any) => void - - 接口调用结束的回调函数(调用成功、失败都会执行)

# accessSync(path: string): void;

FileSystemManager.access 的同步版本

# accessSync 兼容性
HarmonyOS
HBuilderX 4.51
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
path string - - 要判断是否存在的文件/目录路径 (本地路径)

# rename(options: RenameOptions): void;

重命名文件。可以把文件从 oldPath 移动到 newPath

# rename 兼容性
HarmonyOS
HBuilderX 4.51
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
options RenameOptions - - -
名称 类型 必填 默认值 兼容性 描述
oldPath string.URIString - - 源文件路径,支持本地路径
newPath string.URIString - - 新文件路径,支持本地路径
success (res: FileManagerSuccessResult) => void - - 接口调用的回调函数
fail (res: IFileSystemManagerFail) => void - - 接口调用失败的回调函数
complete (res: any) => void - - 接口调用结束的回调函数(调用成功、失败都会执行)

# renameSync(oldPath: string, newPath: string): void;

FileSystemManager.rename 的同步版本

# renameSync 兼容性
HarmonyOS
HBuilderX 4.51
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
oldPath string - - 源文件路径,支持本地路径
newPath string - - 新文件路径,支持本地路径

# copyFile(options: CopyFileOptions): void;

复制文件

# copyFile 兼容性
HarmonyOS
HBuilderX 4.51
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
options CopyFileOptions - - -
名称 类型 必填 默认值 兼容性 描述
srcPath string.URIString - - 源文件路径,支持本地路径
destPath string.URIString - - 新文件路径,支持本地路径
success (res: FileManagerSuccessResult) => void - - 接口调用的回调函数
fail (res: IFileSystemManagerFail) => void - - 接口调用失败的回调函数
complete (res: any) => void - - 接口调用结束的回调函数(调用成功、失败都会执行)

# copyFileSync(srcPath: string, destPath: string): void;

FileSystemManager.copyFile 的同步版本

# copyFileSync 兼容性
HarmonyOS
HBuilderX 4.51
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
srcPath string - - 源文件路径,支持本地路径
destPath string - - 新文件路径,支持本地路径

# getFileInfo(options: GetFileInfoOptions): void;

获取该本地临时文件 或 本地缓存文件 信息

# getFileInfo 兼容性
HarmonyOS
HBuilderX 4.51
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
options GetFileInfoOptions - - -
名称 类型 必填 默认值 兼容性 描述
filePath string.URIString - - 要读取的文件路径 (本地路径)
digestAlgorithm string - - 计算文件摘要的算法
success (res: GetFileInfoSuccessResult) => void - - 接口调用的回调函数
fail (res: IFileSystemManagerFail) => void - - 接口调用失败的回调函数
complete (res: any) => void - - 接口调用结束的回调函数(调用成功、失败都会执行)
# GetFileInfoSuccessResult 的属性值
名称 类型 必填 默认值 兼容性 描述
digest string - - 按照传入的 digestAlgorithm 计算得出的的文件摘要
size number - - 文件大小,以字节为单位

# stat(options: StatOptions): void;

获取文件 Stats 对象

# stat 兼容性
HarmonyOS
HBuilderX 4.51
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
options StatOptions - - -
名称 类型 必填 默认值 兼容性 描述
path string.URIString - - 文件/目录路径 (本地路径)
recursive boolean - - 是否递归获取目录下的每个文件的 Stats 信息
success (res: StatSuccessResult) => void - - 接口调用的回调函数
fail (res: IFileSystemManagerFail) => void - - 接口调用失败的回调函数
complete (res: any) => void - - 接口调用结束的回调函数(调用成功、失败都会执行)
# StatSuccessResult 的属性值
名称 类型 必填 默认值 兼容性 描述
stats Array<FileStats> - - -
# Stats 的方法
# isDirectory(): boolean;

判断当前文件是否一个目录

# isDirectory 兼容性
HarmonyOS
HBuilderX 4.51
# 返回值
类型
boolean
# isFile: isDirectory(): boolean;

判断当前文件是否一个普通文件

# isFile 兼容性
HarmonyOS
HBuilderX 4.51
# 返回值
类型
boolean

# statSync(path : string, recursive : boolean) : Array<Map<string, any>>;

FileSystemManager.stat 的同步版本

# statSync 兼容性
HarmonyOS
HBuilderX 4.51
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
path string - - 文件/目录路径 (本地路径)
recursive boolean - - 是否递归获取目录下的每个文件的 Stats 信息
# 返回值
类型
Array<Map<string, any>>

# appendFile(options: AppendFileOptions): void;

在文件结尾追加内容

# appendFile 兼容性
HarmonyOS
HBuilderX 4.51
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
options AppendFileOptions - - -
名称 类型 必填 默认值 兼容性 描述
filePath string.URIString - - 要追加内容的文件路径 (本地路径)
encoding string - - 指定写入文件的字符编码
支持:ascii base64 utf-8
只在 data 类型是 String 时有效
data string | ArrayBuffer -
要追加的文本或二进制数据,类型为 String 或 ArrayBuffer,以前类型是string,iOS平台4.61、Android平台4.31及以后支持arraybuffer
success (res: FileManagerSuccessResult) => void - - 接口调用的回调函数
fail (res: IFileSystemManagerFail) => void - - 接口调用失败的回调函数
complete (res: any) => void - - 接口调用结束的回调函数(调用成功、失败都会执行)

# appendFileSync(filePath: string, data: string | ArrayBuffer, encoding?: string): void;

FileSystemManager.appendFile 的同步版本

# appendFileSync 兼容性
HarmonyOS
HBuilderX 4.51
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
filePath string - - 要追加内容的文件路径 (本地路径)
data string | ArrayBuffer - - 要追加的文本或二进制数据,类型为 String 或 ArrayBuffer,Android平台4.31、iOS平台4.61之前前类型是string,Android平台4.31、iOS平台4.61起支持ArrayBuffer
encoding string - - 指定写入文件的字符编码支持:ascii base64 utf-8,只在 data 类型是 String 时有效

# saveFile(options: SaveFileOptions): void;

保存临时文件到本地。此接口会移动临时文件,因此调用成功后,tempFilePath 将不可用。

# saveFile 兼容性
HarmonyOS
HBuilderX 4.51
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
options SaveFileOptions - - -
名称 类型 必填 默认值 兼容性 描述
tempFilePath string.URIString - - 临时存储文件路径 (本地路径)
filePath string.URIString - - 要存储的文件路径 (本地路径)
success (res: SaveFileSuccessResult) => void - - 接口调用的回调函数
fail (res: IFileSystemManagerFail) => void - - 接口调用失败的回调函数
complete (res: any) => void - - 接口调用结束的回调函数(调用成功、失败都会执行)
# SaveFileSuccessResult 的属性值
名称 类型 必填 默认值 兼容性 描述
savedFilePath string - - 存储后的文件路径 (本地路径)

# saveFileSync(tempFilePath: string, filePath: string | null): string;

FileSystemManager.saveFile 的同步版本

# saveFileSync 兼容性
HarmonyOS
HBuilderX 4.51
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
tempFilePath string - - 临时存储文件路径 (本地路径)
filePath string - - 要存储的文件路径 (本地路径)
# 返回值
类型
string

# removeSavedFile(options: RemoveSavedFileOptions): void;

删除该小程序下已保存的本地缓存文件

# removeSavedFile 兼容性
HarmonyOS
HBuilderX 4.51
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
options RemoveSavedFileOptions - - -
名称 类型 必填 默认值 兼容性 描述
filePath string.URIString - - 需要删除的文件路径 (本地路径)
success (res: FileManagerSuccessResult) => void - - 接口调用的回调函数
fail (res: IFileSystemManagerFail) => void - - 接口调用失败的回调函数
complete (res: any) => void - - 接口调用结束的回调函数(调用成功、失败都会执行)

# unzip(options: UnzipFileOptions): void;

解压文件

# unzip 兼容性
HarmonyOS
HBuilderX 4.51
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
options UnzipFileOptions - - -
名称 类型 必填 默认值 兼容性 描述
zipFilePath string - - 源文件路径,支持本地路径, 只可以是 zip 压缩文件
targetPath string - - 目标目录路径, 支持本地路径
success (res: FileManagerSuccessResult) => void - - 接口调用的回调函数
fail (res: IFileSystemManagerFail) => void - - 接口调用失败的回调函数
complete (res: any) => void - - 接口调用结束的回调函数(调用成功、失败都会执行)

# getSavedFileList(options: GetSavedFileListOptions): void;

获取该已保存的本地缓存文件列表

# getSavedFileList 兼容性
HarmonyOS
HBuilderX 4.51
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
options GetSavedFileListOptions - - -
名称 类型 必填 默认值 兼容性 描述
success (res: GetSavedFileListResult) => void - - 接口调用的回调函数
fail (res: IFileSystemManagerFail) => void - - 接口调用失败的回调函数
complete (res: any) => void - - 接口调用结束的回调函数(调用成功、失败都会执行)
# GetSavedFileListResult 的属性值
名称 类型 必填 默认值 兼容性 描述
fileList Array<string> - - 文件数组

# truncate(options: TruncateFileOptions): void;

对文件内容进行截断操作

# truncate 兼容性
HarmonyOS
HBuilderX 4.51
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
options TruncateFileOptions - - -
名称 类型 必填 默认值 兼容性 描述
filePath string.URIString - - 要截断的文件路径 (本地路径)
length number - - 截断位置,默认0。如果 length 小于文件长度(字节),则只有前面 length 个字节会保留在文件中,其余内容会被删除;
如果 length 大于文件长度,不做处理
success (res: FileManagerSuccessResult) => void - - 接口调用的回调函数
fail (res: IFileSystemManagerFail) => void - - 接口调用失败的回调函数
complete (res: any) => void - - 接口调用结束的回调函数(调用成功、失败都会执行)

# truncateSync(filePath: string, length?: number): void;

对文件内容进行截断操作 (truncate 的同步版本)

# truncateSync 兼容性
HarmonyOS
HBuilderX 4.51
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
filePath string - - 要截断的文件路径 (本地路径)
length number - - 截断位置,默认0。如果 length 小于文件长度(字节),则只有前面 length 个字节会保留在文件中,其余内容会被删除;如果 length 大于文件长度,不做处理

# readCompressedFile(options: ReadCompressedFileOptions): void;

读取指定压缩类型的本地文件内容

# readCompressedFile 兼容性
HarmonyOS
HBuilderX 4.51
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
options ReadCompressedFileOptions - - -
名称 类型 必填 默认值 兼容性 描述
filePath string.URIString - - 要读取的文件的路径 (本地用户文件或代码包文件),app-android平台支持代码包文件目录
compressionAlgorithm string - - 文件压缩类型,目前仅支持 'br'。
success (res: ReadCompressedFileResult) => void - - 接口调用的回调函数
fail (res: IFileSystemManagerFail) => void - - 接口调用失败的回调函数
complete (res: any) => void - - 接口调用结束的回调函数(调用成功、失败都会执行)
# ReadCompressedFileResult 的属性值
名称 类型 必填 默认值 兼容性 描述
data string - - -

# readCompressedFileSync(filePath: string, compressionAlgorithm: string): string

同步读取指定压缩类型的本地文件内容

# readCompressedFileSync 兼容性
HarmonyOS
HBuilderX 4.51
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
filePath string - - 要读取的文件的路径 (本地用户文件或代码包文件),app-android平台支持代码包文件目录
compressionAlgorithm string - - 文件压缩类型,目前仅支持 'br'。
# 返回值
类型
string

# open(options: OpenFileOptions): void;

打开文件,返回文件描述符

# open 兼容性
HarmonyOS
HBuilderX 4.51
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
options OpenFileOptions - - -
名称 类型 必填 默认值 兼容性 描述
filePath string.URIString - - 要追加内容的文件路径 (本地路径)
flag string - - 文件系统标志,默认值: 'r'
success (res: OpenFileSuccessResult) => void - - 接口调用的回调函数
fail (res: IFileSystemManagerFail) => void - - 接口调用失败的回调函数
complete (res: any) => void - - 接口调用结束的回调函数(调用成功、失败都会执行)
# OpenFileSuccessResult 的属性值
名称 类型 必填 默认值 兼容性 描述
fd string - - 文件描述符

# openSync(options: OpenFileSyncOptions): string;

同步打开文件,返回文件描述符

# openSync 兼容性
HarmonyOS
HBuilderX 4.51
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
options OpenFileSyncOptions - - -
名称 类型 必填 默认值 兼容性 描述
filePath string.URIString - - 要追加内容的文件路径 (本地路径)
flag string - - 文件系统标志,默认值: 'r'
# 返回值
类型
string

# write(options: WriteOptions): void;

写入文件

# write 兼容性
HarmonyOS
HBuilderX 4.41
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
options WriteOptions - - -
名称 类型 必填 默认值 兼容性 描述
fd string - - 文件描述符。fd 通过 FileSystemManager.open 或 FileSystemManager.openSync 接口获得
data string | ArrayBuffer - - 写入的内容,类型为 String 或 ArrayBuffer,以前类型是string,iOS平台4.61、Android平台4.31及以后支持ArrayBuffer
offset number 0
Android平台4.31及以后版本新增,只在 data 类型是 ArrayBuffer 时有效,决定 ArrayBuffer 中要被写入的部位,即 ArrayBuffer 中的索引,默认0
length number -
Android平台4.31及以后版本新增,只在 data 类型是 ArrayBuffer 时有效,指定要写入的字节数,默认为 ArrayBuffer 从0开始偏移 offset 个字节后剩余的字节数
position number -
Andorid平台4.31及以后版本新增,指定文件开头的偏移量,即数据要被写入的位置。当 position 不传或者传入非 Number 类型的值时,数据会被写入当前指针所在位置。
encoding string - - 只在 data 类型是 String 时有效,指定写入文件的字符编码,默认为 utf8
支持:ascii base64 utf-8
success (res: WriteResult) => void - - 接口调用的回调函数
fail (res: IFileSystemManagerFail) => void - - 接口调用失败的回调函数
complete (res: any) => void - - 接口调用结束的回调函数(调用成功、失败都会执行)
# WriteResult 的属性值
名称 类型 必填 默认值 兼容性 描述
bytesWritten number - - 实际被写入到文件中的字节数(注意,被写入的字节数不一定与被写入的字符串字符数相同)

# writeSync(options: WriteSyncOptions): WriteResult;

同步写入文件

# writeSync 兼容性
HarmonyOS
HBuilderX 4.41
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
options WriteSyncOptions - - -
名称 类型 必填 默认值 兼容性 描述
fd string - - 文件描述符。fd 通过 FileSystemManager.open 或 FileSystemManager.openSync 接口获得
data string | ArrayBuffer -
写入的内容,类型为 String 或 ArrayBuffer,在4.31以前类型是string,4.31起为了同时支持ArrayBuffer
encoding string - - 只在 data 类型是 String 时有效,指定写入文件的字符编码,默认为 utf8
支持:ascii base64 utf-8
length number -
只在 data 类型是 ArrayBuffer 时有效,指定要写入的字节数,默认为 arrayBuffer 从0开始偏移 offset 个字节后剩余的字节数 ,4.31及以后版本新增
offset number -
只在 data 类型是 ArrayBuffer 时有效,决定 arrayBuffe 中要被写入的部位,即 arrayBuffer 中的索引,默认0,4.31及以后版本新增
position number -
指定文件开头的偏移量,即数据要被写入的位置。当 position 不传或者传入非 Number 类型的值时,数据会被写入当前指针所在位置。4.31及以后版本新增
# 返回值
类型
WriteResult

# close(options: CloseOptions): void;

关闭文件

# close 兼容性
HarmonyOS
HBuilderX 4.51
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
options CloseOptions - - -
名称 类型 必填 默认值 兼容性 描述
fd string - - 需要被关闭的文件描述符。fd 通过 FileSystemManager.open 或 FileSystemManager.openSync 接口获得
success (res: FileManagerSuccessResult) => void - - 接口调用的回调函数
fail (res: IFileSystemManagerFail) => void - - 接口调用失败的回调函数
complete (res: any) => void - - 接口调用结束的回调函数(调用成功、失败都会执行)

# closeSync(options: CloseSyncOptions): void;

同步关闭文件

# closeSync 兼容性
HarmonyOS
HBuilderX 4.51
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
options CloseSyncOptions - - -
名称 类型 必填 默认值 兼容性 描述
fd string - - 需要被关闭的文件描述符。fd 通过 FileSystemManager.open 或 FileSystemManager.openSync 接口获得

# fstat(options: FStatOptions): void;

获取文件的状态信息

# fstat 兼容性
HarmonyOS
HBuilderX 4.51
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
options FStatOptions - - -
名称 类型 必填 默认值 兼容性 描述
fd string - - 文件描述符。fd 通过 FileSystemManager.open 或 FileSystemManager.openSync 接口获得
success (res: FStatSuccessResult) => void - - 接口调用的回调函数
fail (res: IFileSystemManagerFail) => void - - 接口调用失败的回调函数
complete (res: any) => void - - 接口调用结束的回调函数(调用成功、失败都会执行)
# FStatSuccessResult 的属性值
名称 类型 必填 默认值 兼容性 描述
stats Stats - - Stats 对象,包含了文件的状态信息

# fstatSync(options: FStatSyncOptions): Stats;

同步获取文件的状态信息

# fstatSync 兼容性
HarmonyOS
HBuilderX 4.51
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
options FStatSyncOptions - - -
名称 类型 必填 默认值 兼容性 描述
fd string - - 文件描述符。fd 通过 FileSystemManager.open 或 FileSystemManager.openSync 接口获得
# 返回值
类型 描述
Stats Stats 对象,包含了文件的状态信息

# ftruncate(options: FTruncateFileOptions): void;

对文件内容进行截断操作

# ftruncate 兼容性
HarmonyOS
HBuilderX 4.51
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
options FTruncateFileOptions - - -
名称 类型 必填 默认值 兼容性 描述
fd string - - 文件描述符。fd 通过 FileSystemManager.open 或 FileSystemManager.openSync 接口获得
length number - - 截断位置,默认0。如果 length 小于文件长度(字节),则只有前面 length 个字节会保留在文件中,其余内容会被删除;
如果 length 大于文件长度,不做处理
success (res: FileManagerSuccessResult) => void - - 接口调用的回调函数
fail (res: IFileSystemManagerFail) => void - - 接口调用失败的回调函数
complete (res: any) => void - - 接口调用结束的回调函数(调用成功、失败都会执行)

# ftruncateSync(options: FTruncateFileSyncOptions): void;

同步对文件内容进行截断操作

# ftruncateSync 兼容性
HarmonyOS
HBuilderX 4.51
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
options FTruncateFileSyncOptions - - -
名称 类型 必填 默认值 兼容性 描述
fd string - - 文件描述符。fd 通过 FileSystemManager.open 或 FileSystemManager.openSync 接口获得
length number - - 截断位置,默认0。如果 length 小于文件长度(字节),则只有前面 length 个字节会保留在文件中,其余内容会被删除;
如果 length 大于文件长度,不做处理

# readZipEntry(options: ReadZipEntryOptions): void;

读取压缩包内的文件

# readZipEntry 兼容性
HarmonyOS
HBuilderX 4.51
# 参数 HarmonyOS 兼容性
名称 类型 必填 默认值 兼容性 描述
options ReadZipEntryOptions - - -
名称 类型 必填 默认值 兼容性 描述
filePath string.URIString - - 要读取的压缩包的路径 (本地路径),app-android平台支持代码包文件目录
encoding string - - 统一指定读取文件的字符编码,只在 entries 值为"all"时有效。
4.31及以后版本如果 entries 值为 null 且不传 encoding,则以 ArrayBuffer 格式读取文件的二进制内容
entries Array<EntryItem> - - 要读取的压缩包内的文件列表(当不传入时表示读取压缩包内所有文件)
success (res: EntriesResult) => void - - 接口调用的回调函数
fail (res: IFileSystemManagerFail) => void - - 接口调用失败的回调函数
complete (res: any) => void - - 接口调用结束的回调函数(调用成功、失败都会执行)
# EntriesResult 的属性值
名称 类型 必填 默认值 兼容性 描述
entries Map<string, ZipFileItem> - - 文件路径
result Map<string, ZipFileItem> - - 已废弃,使用 entries

# 其他平台

本页导读