harmony 鸿蒙@ohos.file.statvfs (File System Space Statistics)

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

@ohos.file.statvfs (File System Space Statistics)

This module provides APIs for obtaining file system information, including the total size and free size of a file system, in bytes.

NOTE

The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

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

statfs.getFreeSize

getFreeSize(path:string):Promise<number>

Obtains the free size of the specified file system, in bytes. This API uses a promise to return the result.

System capability: SystemCapability.FileManagement.File.FileIO

Parameters

Name Type Mandatory Description
path string Yes File path of the file system.

Return value

Type Description
Promise<number> Promise used to return the free size obtained, in bytes.

Error codes

For details about the error codes, see Basic File IO Error Codes.

Example

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

  // Obtain the context from the component and ensure that the return value of this.getUIContext().getHostContext() is 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

Obtains the free size of the specified file system, in bytes. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.FileManagement.File.FileIO

Parameters

Name Type Mandatory Description
path string Yes File path of the file system.
callback AsyncCallback<number> Yes Callback used to return the free size obtained, in bytes.

Error codes

For details about the error codes, see Basic File IO Error Codes.

Example

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

  // Obtain the context from the component and ensure that the return value of this.getUIContext().getHostContext() is 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

Obtains the free size of the specified file system, in bytes. This API returns the result synchronously.

System capability: SystemCapability.FileManagement.File.FileIO

Parameters

Name Type Mandatory Description
path string Yes File path of the file system.

Return value

Type Description
number Free size obtained, in bytes.

Error codes

For details about the error codes, see Basic File IO Error Codes.

Example

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

  // Obtain the context from the component and ensure that the return value of this.getUIContext().getHostContext() is 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>

Obtains the total size of the specified file system, in bytes. This API uses a promise to return the result.

System capability: SystemCapability.FileManagement.File.FileIO

Parameters

Name Type Mandatory Description
path string Yes File path of the file system.

Return value

Type Description
Promise<number> Promise used to return the total size obtained, in bytes.

Error codes

For details about the error codes, see Basic File IO Error Codes.

Example

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

  // Obtain the context from the component and ensure that the return value of this.getUIContext().getHostContext() is 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

Obtains the total size of the specified file system, in bytes. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.FileManagement.File.FileIO

Parameters

Name Type Mandatory Description
path string Yes File path of the file system.
callback AsyncCallback<number> Yes Callback used to return the total size obtained, in bytes.

Error codes

For details about the error codes, see Basic File IO Error Codes.

Example

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

  // Obtain the context from the component and ensure that the return value of this.getUIContext().getHostContext() is 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

Obtains the total size of the specified file system, in bytes. This API returns the result synchronously.

System capability: SystemCapability.FileManagement.File.FileIO

Parameters

Name Type Mandatory Description
path string Yes File path of the file system.

Return value

Type Description
number Total size obtained, in bytes.

Error codes

For details about the error codes, see Basic File IO Error Codes.

Example

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

  // Obtain the context from the component and ensure that the return value of this.getUIContext().getHostContext() is 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 鸿蒙File Management Error Codes

harmony 鸿蒙FileShare

harmony 鸿蒙FileUri

harmony 鸿蒙@ohos.application.BackupExtensionAbility (Backup and Restore Extension Capability) (System API)

0  赞