鸿蒙API14开发【@ohos.file.statvfs (文件系统空间统计)】文件基础服务

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

说明

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

导入模块

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

statfs.getFreeSize

getFreeSize(path:string):Promise

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

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

参数:

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

返回值:

类型说明
PromisePromise对象,返回空闲字节数。

示例:

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

let context = getContext(this) 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): void

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

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

参数:

参数名类型必填说明
pathstring需要查询的文件系统的文件路径。
callbackAsyncCallback异步获取空闲字节数之后的回调。

示例:

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

let context = getContext(this) 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

参数:

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

返回值:

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

示例:

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

let context = getContext(this) as common.UIAbilityContext;
let path = context.filesDir;
let number = statfs.getFreeSizeSync(path);
console.info("getFreeSizeSync succeed, Size: " + number);

statfs.getTotalSize

getTotalSize(path: string): Promise

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

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

参数:

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

返回值:

类型说明
PromisePromise对象,返回总字节数。

示例:

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

let context = getContext(this) 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): void

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

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

参数:

参数名类型必填说明
pathstring需要查询的文件系统的文件路径。
callbackAsyncCallback异步获取总字节数之后的回调。

示例:

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

let context = getContext(this) 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

参数:

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

返回值:

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

示例:

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

let context = getContext(this) as common.UIAbilityContext;
let path = context.filesDir;
let number = statfs.getTotalSizeSync(path);
console.info("getTotalSizeSync succeed, Size: " + number);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值