harmony 鸿蒙@ohos.file.statvfs (文件系统空间统计)

  • 2025-06-12
  • 浏览 (1)

@ohos.file.statvfs (文件系统空间统计)

该模块提供文件系统相关存储信息的功能:向应用程序提供获取文件系统总字节数、空闲字节数的JS接口。

说明:

本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

导入模块

import { statfs } from '@kit.CoreFileKit';

statfs.getFreeSize

getFreeSize(path:string):Promise<number>

异步方法获取指定文件系统空闲字节数,以Promise形式返回结果。

系统能力:SystemCapability.FileManagement.File.FileIO

参数:

参数名 类型 必填 说明
path string 需要查询的文件系统的文件路径。

返回值:

类型 说明
Promise<number> Promise对象,返回空闲字节数。

错误码:

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

示例:

  import { BusinessError } from '@kit.BasicServicesKit';
  import { common } from '@kit.AbilityKit';

  // 请在组件内获取context,确保this.getUIContext().getHostContext()返回结果为UIAbilityContext
  let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
  let path = context.filesDir;
  statfs.getFreeSize(path).then((number: number) => {
    console.info("getFreeSize succeed, Size: " + number);
  }).catch((err: BusinessError) => {
    console.error("getFreeSize failed with error message: " + err.message + ", error code: " + err.code);
  });

statfs.getFreeSize

getFreeSize(path:string, callback:AsyncCallback<number>): void

异步方法获取指定文件系统空闲字节数,使用callback形式返回结果。

系统能力:SystemCapability.FileManagement.File.FileIO

参数:

参数名 类型 必填 说明
path string 需要查询的文件系统的文件路径。
callback AsyncCallback<number> 异步获取空闲字节数之后的回调。

错误码:

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

示例:

  import { BusinessError } from '@kit.BasicServicesKit';
  import { common } from '@kit.AbilityKit';

  // 请在组件内获取context,确保this.getUIContext().getHostContext()返回结果为UIAbilityContext
  let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
  let path = context.filesDir;
  statfs.getFreeSize(path, (err: BusinessError, number: number) => {
    if (err) {
      console.error("getFreeSize failed with error message: " + err.message + ", error code: " + err.code);
    } else {
      console.info("getFreeSize succeed, Size: " + number);
    }
  });

statfs.getFreeSizeSync10+

getFreeSizeSync(path:string): number

以同步方法获取指定文件系统空闲字节数。

系统能力:SystemCapability.FileManagement.File.FileIO

参数:

参数名 类型 必填 说明
path string 需要查询的文件系统的文件路径。

返回值:

类型 说明
number 返回空闲字节数。

错误码:

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

示例:

  import { common } from '@kit.AbilityKit';

  // 请在组件内获取context,确保this.getUIContext().getHostContext()返回结果为UIAbilityContext
  let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
  let path = context.filesDir;
  let number = statfs.getFreeSizeSync(path);
  console.info("getFreeSizeSync succeed, Size: " + number);

statfs.getTotalSize

getTotalSize(path: string): Promise<number>

异步方法获取指定文件系统总字节数,以Promise形式返回结果。

系统能力:SystemCapability.FileManagement.File.FileIO

参数:

参数名 类型 必填 说明
path string 需要查询的文件系统的文件路径。

返回值:

类型 说明
Promise<number> Promise对象,返回总字节数。

错误码:

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

示例:

  import { BusinessError } from '@kit.BasicServicesKit';
  import { common } from '@kit.AbilityKit';

  // 请在组件内获取context,确保this.getUIContext().getHostContext()返回结果为UIAbilityContext
  let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
  let path = context.filesDir;
  statfs.getTotalSize(path).then((number: number) => {
    console.info("getTotalSize succeed, Size: " + number);
  }).catch((err: BusinessError) => {
    console.error("getTotalSize failed with error message: " + err.message + ", error code: " + err.code);
  });

statfs.getTotalSize

getTotalSize(path: string, callback: AsyncCallback<number>): void

异步方法获取指定文件系统总字节数,使用callback形式返回结果。

系统能力:SystemCapability.FileManagement.File.FileIO

参数:

参数名 类型 必填 说明
path string 需要查询的文件系统的文件路径。
callback AsyncCallback<number> 异步获取总字节数之后的回调。

错误码:

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

示例:

  import { BusinessError } from '@kit.BasicServicesKit';
  import { common } from '@kit.AbilityKit';

  // 请在组件内获取context,确保this.getUIContext().getHostContext()返回结果为UIAbilityContext
  let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
  let path = context.filesDir;
  statfs.getTotalSize(path, (err: BusinessError, number: number) => {
    if (err) {
      console.error("getTotalSize failed with error message: " + err.message + ", error code: " + err.code);
    } else {
      console.info("getTotalSize succeed, Size: " + number);
    }
  });

statfs.getTotalSizeSync10+

getTotalSizeSync(path: string): number

以同步方法获取指定文件系统总字节数。

系统能力:SystemCapability.FileManagement.File.FileIO

参数:

参数名 类型 必填 说明
path string 需要查询的文件系统的文件路径。

返回值:

类型 说明
number 返回总字节数。

错误码:

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

示例:

  import { common } from '@kit.AbilityKit';

  // 请在组件内获取context,确保this.getUIContext().getHostContext()返回结果为UIAbilityContext
  let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
  let path = context.filesDir;
  let number = statfs.getTotalSizeSync(path);
  console.info("getTotalSizeSync succeed, Size: " + number);

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Core File Kit(文件基础服务)

harmony 鸿蒙Environment

harmony 鸿蒙FileIO

harmony 鸿蒙FileShare_PolicyErrorResult

harmony 鸿蒙FileShare_PolicyInfo

harmony 鸿蒙error_code.h

harmony 鸿蒙文件管理错误码

harmony 鸿蒙FileShare

harmony 鸿蒙FileUri

harmony 鸿蒙@ohos.application.BackupExtensionAbility (备份恢复扩展能力)(系统接口)

0  赞