openharmony 鸿蒙 js-apis-statfs

2025-06-12 浏览 (1)

@ohos.statfs (statfs)

The statfs 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 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
  • The APIs provided by this module are deprecated since API version 9. You are advised to use @ohos.file.statvfs.

Modules to Import

import statfs from '@ohos.statfs';

Statfs.getFreeBytes

getFreeBytes(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

NameTypeMandatoryDescription
pathstringYesPath of the file system.

Return value

TypeDescription
Promise<number>Promise used to return the free size obtained.

Example

import { BusinessError } from '@ohos.base';
let path = "/dev";
statfs.getFreeBytes(path).then((number: number) => {
  console.info("getFreeBytes promise successfully:" + number);
}).catch((err: BusinessError) => {
  console.error("getFreeBytes failed with error:" + JSON.stringify(err));
});

Statfs.getFreeBytes

getFreeBytes(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

NameTypeMandatoryDescription
pathstringYesPath of the file system.
callbackAsyncCallback<number>YesCallback used to return the free size obtained.

Example

import common from '@ohos.app.ability.common';
import { BusinessError } from '@ohos.base';
let context = getContext(this) as common.UIAbilityContext;
let path = context.filesDir;
statfs.getFreeBytes(path, (err: BusinessError, freeBytes:Number) => {
    if (err) {
        console.error('getFreeBytes callback failed');
    } else {
        console.info('getFreeBytes callback success' + freeBytes);
    }
});

Statfs.getTotalBytes

getTotalBytes(path: string): Promise<number>

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

System capability: SystemCapability.FileManagement.File.FileIO

Parameters

NameTypeMandatoryDescription
pathstringYesPath of the file system.

Return value

TypeDescription
Promise<number>Promise used to return the total size obtained.

Example

import { BusinessError } from '@ohos.base';
let path = "/dev";
statfs.getTotalBytes(path).then((number: number) => {
  console.info("getTotalBytes promise successfully:" + number);
}).catch((err: BusinessError) => {
  console.error("getTotalBytes failed with error:" + JSON.stringify(err));
});

Statfs.getTotalBytes

getTotalBytes(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

NameTypeMandatoryDescription
pathstringYesPath of the file system.
callbackAsyncCallback<number>YesCallback used to return the total size obtained.

Example

import common from '@ohos.app.ability.common';
import { BusinessError } from '@ohos.base';
let context = getContext(this) as common.UIAbilityContext;
let path = context.filesDir;
statfs.getTotalBytes(path, (err: BusinessError, totalBytes:Number) => {
    if (err) {
        console.error('getTotalBytes callback failed');
    } else {
        console.info('getTotalBytes callback success' + totalBytes);
    }
});

你可能感兴趣的鸿蒙文章

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)

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/myT5JT