1# @ohos.file.statvfs (文件系统空间统计) 2 3该模块提供文件系统相关存储信息的功能:向应用程序提供获取文件系统总字节数、空闲字节数的JS接口。 4 5> **说明:** 6> 7> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8 9## 导入模块 10 11```ts 12import { statfs } from '@kit.CoreFileKit'; 13``` 14 15## statfs.getFreeSize 16 17getFreeSize(path:string):Promise<number> 18 19异步方法获取指定文件系统空闲字节数,以Promise形式返回结果。 20 21**系统能力**:SystemCapability.FileManagement.File.FileIO 22 23**参数:** 24 25 | 参数名 | 类型 | 必填 | 说明 | 26 | ------ | ------ | ---- | ---------------------------- | 27 | path | string | 是 | 需要查询的文件系统的文件路径。 | 28 29**返回值:** 30 31 | 类型 | 说明 | 32 | --------------------- | -------------- | 33 | Promise<number> | Promise对象,返回空闲字节数。 | 34 35**错误码:** 36 37接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。 38 39**示例:** 40 41 ```ts 42 import { BusinessError } from '@kit.BasicServicesKit'; 43 import { common } from '@kit.AbilityKit'; 44 45 // 请在组件内获取context,确保this.getUIContext().getHostContext()返回结果为UIAbilityContext 46 let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 47 let path = context.filesDir; 48 statfs.getFreeSize(path).then((number: number) => { 49 console.info("getFreeSize succeed, Size: " + number); 50 }).catch((err: BusinessError) => { 51 console.error("getFreeSize failed with error message: " + err.message + ", error code: " + err.code); 52 }); 53 ``` 54 55## statfs.getFreeSize 56 57getFreeSize(path:string, callback:AsyncCallback<number>): void 58 59异步方法获取指定文件系统空闲字节数,使用callback形式返回结果。 60 61**系统能力**:SystemCapability.FileManagement.File.FileIO 62 63**参数:** 64 65 | 参数名 | 类型 | 必填 | 说明 | 66 | -------- | --------------------------- | ---- | ---------------------------- | 67 | path | string | 是 | 需要查询的文件系统的文件路径。 | 68 | callback | AsyncCallback<number> | 是 | 异步获取空闲字节数之后的回调。 | 69 70**错误码:** 71 72接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。 73 74**示例:** 75 76 ```ts 77 import { BusinessError } from '@kit.BasicServicesKit'; 78 import { common } from '@kit.AbilityKit'; 79 80 // 请在组件内获取context,确保this.getUIContext().getHostContext()返回结果为UIAbilityContext 81 let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 82 let path = context.filesDir; 83 statfs.getFreeSize(path, (err: BusinessError, number: number) => { 84 if (err) { 85 console.error("getFreeSize failed with error message: " + err.message + ", error code: " + err.code); 86 } else { 87 console.info("getFreeSize succeed, Size: " + number); 88 } 89 }); 90 ``` 91 92## statfs.getFreeSizeSync<sup>10+</sup> 93 94getFreeSizeSync(path:string): number 95 96以同步方法获取指定文件系统空闲字节数。 97 98**系统能力**:SystemCapability.FileManagement.File.FileIO 99 100**参数:** 101 102 | 参数名 | 类型 | 必填 | 说明 | 103 | ------ | ------ | ---- | ---------------------------- | 104 | path | string | 是 | 需要查询的文件系统的文件路径。 | 105 106**返回值:** 107 108 | 类型 | 说明 | 109 | --------------------- | -------------- | 110 | number | 返回空闲字节数。 | 111 112**错误码:** 113 114接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。 115 116**示例:** 117 118 ```ts 119 import { common } from '@kit.AbilityKit'; 120 121 // 请在组件内获取context,确保this.getUIContext().getHostContext()返回结果为UIAbilityContext 122 let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 123 let path = context.filesDir; 124 let number = statfs.getFreeSizeSync(path); 125 console.info("getFreeSizeSync succeed, Size: " + number); 126 ``` 127 128## statfs.getTotalSize 129 130getTotalSize(path: string): Promise<number> 131 132异步方法获取指定文件系统总字节数,以Promise形式返回结果。 133 134**系统能力**:SystemCapability.FileManagement.File.FileIO 135 136**参数:** 137 138 | 参数名 | 类型 | 必填 | 说明 | 139 | ---- | ------ | ---- | ---------------------------- | 140 | path | string | 是 | 需要查询的文件系统的文件路径。 | 141 142**返回值:** 143 144 | 类型 | 说明 | 145 | --------------------- | ------------ | 146 | Promise<number> | Promise对象,返回总字节数。 | 147 148**错误码:** 149 150接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。 151 152**示例:** 153 154 ```ts 155 import { BusinessError } from '@kit.BasicServicesKit'; 156 import { common } from '@kit.AbilityKit'; 157 158 // 请在组件内获取context,确保this.getUIContext().getHostContext()返回结果为UIAbilityContext 159 let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 160 let path = context.filesDir; 161 statfs.getTotalSize(path).then((number: number) => { 162 console.info("getTotalSize succeed, Size: " + number); 163 }).catch((err: BusinessError) => { 164 console.error("getTotalSize failed with error message: " + err.message + ", error code: " + err.code); 165 }); 166 ``` 167 168## statfs.getTotalSize 169 170getTotalSize(path: string, callback: AsyncCallback<number>): void 171 172异步方法获取指定文件系统总字节数,使用callback形式返回结果。 173 174**系统能力**:SystemCapability.FileManagement.File.FileIO 175 176**参数:** 177 178 | 参数名 | 类型 | 必填 | 说明 | 179 | -------- | --------------------------- | ---- | ---------------------------- | 180 | path | string | 是 | 需要查询的文件系统的文件路径。 | 181 | callback | AsyncCallback<number> | 是 | 异步获取总字节数之后的回调。 | 182 183**错误码:** 184 185接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。 186 187**示例:** 188 189 ```ts 190 import { BusinessError } from '@kit.BasicServicesKit'; 191 import { common } from '@kit.AbilityKit'; 192 193 // 请在组件内获取context,确保this.getUIContext().getHostContext()返回结果为UIAbilityContext 194 let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 195 let path = context.filesDir; 196 statfs.getTotalSize(path, (err: BusinessError, number: number) => { 197 if (err) { 198 console.error("getTotalSize failed with error message: " + err.message + ", error code: " + err.code); 199 } else { 200 console.info("getTotalSize succeed, Size: " + number); 201 } 202 }); 203 ``` 204 205## statfs.getTotalSizeSync<sup>10+</sup> 206 207getTotalSizeSync(path: string): number 208 209以同步方法获取指定文件系统总字节数。 210 211**系统能力**:SystemCapability.FileManagement.File.FileIO 212 213**参数:** 214 215 | 参数名 | 类型 | 必填 | 说明 | 216 | ---- | ------ | ---- | ---------------------------- | 217 | path | string | 是 | 需要查询的文件系统的文件路径。 | 218 219**返回值:** 220 221 | 类型 | 说明 | 222 | --------------------- | ------------ | 223 | number | 返回总字节数。 | 224 225**错误码:** 226 227接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。 228 229**示例:** 230 231 ```ts 232 import { common } from '@kit.AbilityKit'; 233 234 // 请在组件内获取context,确保this.getUIContext().getHostContext()返回结果为UIAbilityContext 235 let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 236 let path = context.filesDir; 237 let number = statfs.getTotalSizeSync(path); 238 console.info("getTotalSizeSync succeed, Size: " + number); 239 ``` 240