1# @ohos.statfs (statfs) 2 3该模块提供文件系统相关存储信息的功能,向应用程序提供获取文件系统总字节数、空闲字节数的JS接口。 4 5> **说明:** 6> 7> - 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8> - 本模块从API version 9开始废弃,建议使用[@ohos.file.statvfs](js-apis-file-statvfs.md)替代。 9 10## 导入模块 11 12```ts 13import statfs from '@ohos.statfs'; 14``` 15 16## Statfs.getFreeBytes 17 18getFreeBytes(path:string):Promise<number> 19 20异步方法获取指定文件系统空闲字节数,以Promise形式返回结果。 21 22**系统能力**:SystemCapability.FileManagement.File.FileIO 23 24**参数:** 25 26 | 参数名 | 类型 | 必填 | 说明 | 27 | ------ | ------ | ---- | ---------------------------- | 28 | path | string | 是 | 需要查询的文件系统的文件路径 | 29 30**返回值:** 31 32 | 类型 | 说明 | 33 | --------------------- | -------------- | 34 | Promise<number> | 返回空闲字节数 | 35 36**示例:** 37 38 ```ts 39 import { BusinessError } from '@ohos.base'; 40 let path = "/dev"; 41 statfs.getFreeBytes(path).then((number: number) => { 42 console.info("getFreeBytes promise successfully:" + number); 43 }).catch((err: BusinessError) => { 44 console.error("getFreeBytes failed with error:" + JSON.stringify(err)); 45 }); 46 ``` 47 48## Statfs.getFreeBytes 49 50getFreeBytes(path:string, callback:AsyncCallback<number>): void 51 52异步方法获取指定文件系统空闲字节数,使用callback形式返回结果。 53 54**系统能力**:SystemCapability.FileManagement.File.FileIO 55 56**参数:** 57 58 | 参数名 | 类型 | 必填 | 说明 | 59 | -------- | --------------------------- | ---- | ---------------------------- | 60 | path | string | 是 | 需要查询的文件系统的文件路径 | 61 | callback | AsyncCallback<number> | 是 | 异步获取空闲字节数之后的回调 | 62 63**示例:** 64 65 ```ts 66 import common from '@ohos.app.ability.common'; 67 import { BusinessError } from '@ohos.base'; 68 let context = getContext(this) as common.UIAbilityContext; 69 let path = context.filesDir; 70 statfs.getFreeBytes(path, (err: BusinessError, freeBytes:Number) => { 71 if (err) { 72 console.error('getFreeBytes callback failed'); 73 } else { 74 console.info('getFreeBytes callback success' + freeBytes); 75 } 76 }); 77 ``` 78 79## Statfs.getTotalBytes 80 81getTotalBytes(path: string): Promise<number> 82 83异步方法获取指定文件系统总字节数,以Promise形式返回结果。 84 85**系统能力**:SystemCapability.FileManagement.File.FileIO 86 87**参数:** 88 89 | 参数名 | 类型 | 必填 | 说明 | 90 | ---- | ------ | ---- | ---------------------------- | 91 | path | string | 是 | 需要查询的文件系统的文件路径 | 92 93**返回值:** 94 95 | 类型 | 说明 | 96 | --------------------- | ------------ | 97 | Promise<number> | 返回总字节数 | 98 99**示例:** 100 101 ```ts 102 import { BusinessError } from '@ohos.base'; 103 let path = "/dev"; 104 statfs.getTotalBytes(path).then((number: number) => { 105 console.info("getTotalBytes promise successfully:" + number); 106 }).catch((err: BusinessError) => { 107 console.error("getTotalBytes failed with error:" + JSON.stringify(err)); 108 }); 109 ``` 110 111## Statfs.getTotalBytes 112 113getTotalBytes(path: string, callback: AsyncCallback<number>): void 114 115异步方法获取指定文件系统总字节数,使用callback形式返回结果。 116 117**系统能力**:SystemCapability.FileManagement.File.FileIO 118 119**参数:** 120 121 | 参数名 | 类型 | 必填 | 说明 | 122 | -------- | --------------------------- | ---- | ---------------------------- | 123 | path | string | 是 | 需要查询的文件系统的文件路径 | 124 | callback | AsyncCallback<number> | 是 | 异步获取总字节数之后的回调 | 125 126**示例:** 127 128 ```ts 129 import common from '@ohos.app.ability.common'; 130 import { BusinessError } from '@ohos.base'; 131 let context = getContext(this) as common.UIAbilityContext; 132 let path = context.filesDir; 133 statfs.getTotalBytes(path, (err: BusinessError, totalBytes:Number) => { 134 if (err) { 135 console.error('getTotalBytes callback failed'); 136 } else { 137 console.info('getTotalBytes callback success' + totalBytes); 138 } 139 }); 140 ``` 141 142