uni.getFileSystemManager()
仅 HarmonyOS 支持
获取文件管理器
getFileSystemManager 兼容性
返回值
FileSystemManager 的方法
readFile(options: ReadFileOptions): void;
读取本地文件内容
readFile 兼容性
参数 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 兼容性
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
filePath | string | 是 | - | - | 文件路径,支持相对地址和绝对地址,app-android平台支持代码包文件目录 |
encoding | string | 否 | - | - | base64 / utf-8,指定读取文件的字符编码,(iOS平台4.61及以后、Android平台4.31及以后)如果不传 encoding,则以 ArrayBuffer 格式读取文件的二进制内容 |
返回值
writeFile(options: WriteFileOptions): void;
写文件
writeFile 兼容性
参数 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 兼容性
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
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 兼容性
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
option | ReadSyncOption | 是 | - | - | - |
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 | arrayBuffer | ArrayBuffer | 是 | - | | 数据写入的缓冲区,必须是 ArrayBuffer 实例 | fd | string | 是 | - | - | 文件描述符。fd 通过 FileSystemManager.open 或 FileSystemManager.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 兼容性
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
filePath | string | 是 | - | - | 文件路径,只支持绝对地址 |
data | string | ArrayBuffer | 是 | - | - | 要写入的文本或二进制数据,Android平台4.31、iOS平台4.61及以后版本支持ArrayBuffer |
encoding | string | 否 | - | - | 指定写入文件的字符编码,支持:ascii base64 utf-8, 默认值是utf-8, 仅在 data 类型是 String 时有效 |
unlink(options: UnLinkOptions): void;
删除文件
unlink 兼容性
参数 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 兼容性
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
filePath | string | 是 | - | - | 文件路径,只支持绝对地址 |
mkdir(options: MkDirOptions): void;
创建目录
mkdir 兼容性
参数 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 兼容性
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
dirPath | string | 是 | - | - | 创建的目录路径 (本地路径) |
recursive | boolean | 是 | - | - | 是否在递归创建该目录的上级目录后再创建该目录。如果对应的上级目录已经存在,则不创建该上级目录。如 dirPath 为 a/b/c/d 且 recursive 为 true,将创建 a 目录,再在 a 目录下创建 b 目录,以此类推直至创建 a/b/c 目录下的 d 目录。 |
rmdir(options: RmDirOptions): void;
删除目录
rmdir 兼容性
参数 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 兼容性
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
dirPath | string | 是 | - | - | 要删除的目录路径 (本地路径) |
recursive | boolean | 是 | - | - | 是否递归删除目录。如果为 true,则删除该目录和该目录下的所有子目录以及文件。 |
readdir(options: ReadDirOptions): void;
读取目录内文件列表
readdir 兼容性
参数 HarmonyOS 兼容性
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
options | ReadDirOptions | 是 | - | - | - |
|
ReadDirSuccessResult 的属性值
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
files | Array<string> | 是 | - | - | - |
readdirSync(dirPath: string): string[] | null;
FileSystemManager.readdir 的同步版本
readdirSync 兼容性
参数 HarmonyOS 兼容性
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
dirPath | string | 是 | - | - | 要读取的目录路径 (本地路径) |
返回值
access(options: AccessOptions): void;
判断文件/目录是否存在
access 兼容性
参数 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 兼容性
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
path | string | 是 | - | - | 要判断是否存在的文件/目录路径 (本地路径) |
rename(options: RenameOptions): void;
重命名文件。可以把文件从 oldPath 移动到 newPath
rename 兼容性
参数 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 兼容性
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
oldPath | string | 是 | - | - | 源文件路径,支持本地路径 |
newPath | string | 是 | - | - | 新文件路径,支持本地路径 |
copyFile(options: CopyFileOptions): void;
复制文件
copyFile 兼容性
参数 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 兼容性
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
srcPath | string | 是 | - | - | 源文件路径,支持本地路径 |
destPath | string | 是 | - | - | 新文件路径,支持本地路径 |
getFileInfo(options: GetFileInfoOptions): void;
获取该本地临时文件 或 本地缓存文件 信息
getFileInfo 兼容性
参数 HarmonyOS 兼容性
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
options | GetFileInfoOptions | 是 | - | - | - |
|
GetFileInfoSuccessResult 的属性值
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
digest | string | 是 | - | - | 按照传入的 digestAlgorithm 计算得出的的文件摘要 |
size | number | 是 | - | - | 文件大小,以字节为单位 |
stat(options: StatOptions): void;
获取文件 Stats 对象
stat 兼容性
参数 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 兼容性
返回值
isFile: isDirectory(): boolean;
判断当前文件是否一个普通文件
isFile 兼容性
返回值
statSync(path : string, recursive : boolean) : Array<Map<string, any>>;
FileSystemManager.stat 的同步版本
statSync 兼容性
参数 HarmonyOS 兼容性
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
path | string | 是 | - | - | 文件/目录路径 (本地路径) |
recursive | boolean | 是 | - | - | 是否递归获取目录下的每个文件的 Stats 信息 |
返回值
类型 |
Array<Map<string, any>> |
appendFile(options: AppendFileOptions): void;
在文件结尾追加内容
appendFile 兼容性
参数 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 兼容性
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
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 兼容性
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
options | SaveFileOptions | 是 | - | - | - |
|
SaveFileSuccessResult 的属性值
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
savedFilePath | string | 是 | - | - | 存储后的文件路径 (本地路径) |
saveFileSync(tempFilePath: string, filePath: string | null): string;
FileSystemManager.saveFile 的同步版本
saveFileSync 兼容性
参数 HarmonyOS 兼容性
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
tempFilePath | string | 是 | - | - | 临时存储文件路径 (本地路径) |
filePath | string | 否 | - | - | 要存储的文件路径 (本地路径) |
返回值
removeSavedFile(options: RemoveSavedFileOptions): void;
删除该小程序下已保存的本地缓存文件
removeSavedFile 兼容性
参数 HarmonyOS 兼容性
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
options | RemoveSavedFileOptions | 是 | - | - | - |
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 | filePath | string.URIString | 是 | - | - | 需要删除的文件路径 (本地路径) | success | (res: FileManagerSuccessResult) => void | 否 | - | - | 接口调用的回调函数 | fail | (res: IFileSystemManagerFail) => void | 否 | - | - | 接口调用失败的回调函数 | complete | (res: any) => void | 否 | - | - | 接口调用结束的回调函数(调用成功、失败都会执行) |
|
unzip(options: UnzipFileOptions): void;
解压文件
unzip 兼容性
参数 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 兼容性
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
options | GetSavedFileListOptions | 是 | - | - | - |
|
GetSavedFileListResult 的属性值
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
fileList | Array<string> | 是 | - | - | 文件数组 |
truncate(options: TruncateFileOptions): void;
对文件内容进行截断操作
truncate 兼容性
参数 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 兼容性
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
filePath | string | 是 | - | - | 要截断的文件路径 (本地路径) |
length | number | 否 | - | - | 截断位置,默认0。如果 length 小于文件长度(字节),则只有前面 length 个字节会保留在文件中,其余内容会被删除;如果 length 大于文件长度,不做处理 |
readCompressedFile(options: ReadCompressedFileOptions): void;
读取指定压缩类型的本地文件内容
readCompressedFile 兼容性
参数 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 兼容性
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
filePath | string | 是 | - | - | 要读取的文件的路径 (本地用户文件或代码包文件),app-android平台支持代码包文件目录 |
compressionAlgorithm | string | 是 | - | - | 文件压缩类型,目前仅支持 'br'。 |
返回值
open(options: OpenFileOptions): void;
打开文件,返回文件描述符
open 兼容性
参数 HarmonyOS 兼容性
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
options | OpenFileOptions | 是 | - | - | - |
|
OpenFileSuccessResult 的属性值
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
fd | string | 是 | - | - | 文件描述符 |
openSync(options: OpenFileSyncOptions): string;
同步打开文件,返回文件描述符
openSync 兼容性
参数 HarmonyOS 兼容性
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
options | OpenFileSyncOptions | 是 | - | - | - |
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 | filePath | string.URIString | 是 | - | - | 要追加内容的文件路径 (本地路径) | flag | string | 是 | - | - | 文件系统标志,默认值: 'r' |
|
返回值
write(options: WriteOptions): void;
写入文件
write 兼容性
参数 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 兼容性
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
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及以后版本新增 |
|
返回值
close(options: CloseOptions): void;
关闭文件
close 兼容性
参数 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 兼容性
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
options | CloseSyncOptions | 是 | - | - | - |
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 | fd | string | 是 | - | - | 需要被关闭的文件描述符。fd 通过 FileSystemManager.open 或 FileSystemManager.openSync 接口获得 |
|
fstat(options: FStatOptions): void;
获取文件的状态信息
fstat 兼容性
参数 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 兼容性
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
options | FStatSyncOptions | 是 | - | - | - |
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 | fd | string | 是 | - | - | 文件描述符。fd 通过 FileSystemManager.open 或 FileSystemManager.openSync 接口获得 |
|
返回值
类型 | 描述 |
Stats | Stats 对象,包含了文件的状态信息 |
ftruncate(options: FTruncateFileOptions): void;
对文件内容进行截断操作
ftruncate 兼容性
参数 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 兼容性
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
options | FTruncateFileSyncOptions | 是 | - | - | - |
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 | fd | string | 是 | - | - | 文件描述符。fd 通过 FileSystemManager.open 或 FileSystemManager.openSync 接口获得 | length | number | 是 | - | - | 截断位置,默认0。如果 length 小于文件长度(字节),则只有前面 length 个字节会保留在文件中,其余内容会被删除; 如果 length 大于文件长度,不做处理 |
|
readZipEntry(options: ReadZipEntryOptions): void;
读取压缩包内的文件
readZipEntry 兼容性
参数 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 |
其他平台
- 微信小程序平台,规范详情
- 抖音小程序平台,规范详情
- QQ小程序平台,规范详情
- app平台,目前uni-app x支持本API,另见,但uni-app js引擎版还不支持,可部分替代的方案是plus.io,另见
- 鸿蒙元服务平台,详情