1# @ohos.file.statvfs (文件系统空间统计) 2<!--Kit: Core File Kit--> 3<!--Subsystem: FileManagement--> 4<!--Owner: @wangke25; @gsl_1234; @wuchengjun5--> 5<!--Designer: @gsl_1234; @wangke25--> 6<!--Tester: @liuhonggang123; @yue-ye2; @juxiaopang--> 7<!--Adviser: @foryourself--> 8 9该模块提供文件系统相关存储信息的功能:向应用程序提供获取文件系统总字节数、空闲字节数的JS接口。 10 11> **说明:** 12> 13> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 14 15## 导入模块 16 17```ts 18import { statfs } from '@kit.CoreFileKit'; 19``` 20 21## statfs.getFreeSize 22 23getFreeSize(path:string): Promise<number> 24 25异步方法获取指定文件系统空闲字节数,以Promise形式返回结果。 26 27**系统能力**:SystemCapability.FileManagement.File.FileIO 28 29**参数:** 30 31 | 参数名 | 类型 | 必填 | 说明 | 32 | ------ | ------ | ---- | ---------------------------- | 33 | path | string | 是 | 需要查询的文件系统的文件路径。 | 34 35**返回值:** 36 37 | 类型 | 说明 | 38 | --------------------- | -------------- | 39 | Promise<number> | Promise对象,返回空闲字节数。 | 40 41**错误码:** 42 43接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。 44 45| 错误码ID | 错误信息 | 46| -------- | -------- | 47| 13900002 | No such file or directory. | 48| 13900004 | Interrupted system call. | 49| 13900005 | I/O error. | 50| 13900008 | Bad file descriptor. | 51| 13900011 | Out of memory. | 52| 13900012 | Permission denied. | 53| 13900013 | Bad address. | 54| 13900018 | Not a directory. | 55| 13900030 | File name too long. | 56| 13900031 | Function not implemented. | 57| 13900033 | Too many symbolic links encountered. | 58| 13900038 | Value too large for defined data type. | 59| 13900042 | Unknown error. | 60 61**示例:** 62 63<!--code_no_check--> 64 ```ts 65 import { BusinessError } from '@kit.BasicServicesKit'; 66 import { common } from '@kit.AbilityKit'; 67 68 // 请在组件内获取context,确保this.getUIContext().getHostContext()返回结果为UIAbilityContext 69 let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 70 let path = context.filesDir; 71 statfs.getFreeSize(path).then((number: number) => { 72 console.info("getFreeSize succeed, Size: " + number); 73 }).catch((err: BusinessError) => { 74 console.error("getFreeSize failed with error message: " + err.message + ", error code: " + err.code); 75 }); 76 ``` 77 78## statfs.getFreeSize 79 80getFreeSize(path:string, callback:AsyncCallback<number>): void 81 82异步方法获取指定文件系统空闲字节数,使用callback形式返回结果。 83 84**系统能力**:SystemCapability.FileManagement.File.FileIO 85 86**参数:** 87 88 | 参数名 | 类型 | 必填 | 说明 | 89 | -------- | --------------------------- | ---- | ---------------------------- | 90 | path | string | 是 | 需要查询的文件系统的文件路径。 | 91 | callback | AsyncCallback<number> | 是 | 异步获取空闲字节数之后的回调。 | 92 93**错误码:** 94 95接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。 96 97| 错误码ID | 错误信息 | 98| -------- | -------- | 99| 13900002 | No such file or directory. | 100| 13900004 | Interrupted system call. | 101| 13900005 | I/O error. | 102| 13900008 | Bad file descriptor. | 103| 13900011 | Out of memory. | 104| 13900012 | Permission denied. | 105| 13900013 | Bad address. | 106| 13900018 | Not a directory. | 107| 13900030 | File name too long. | 108| 13900031 | Function not implemented. | 109| 13900033 | Too many symbolic links encountered. | 110| 13900038 | Value too large for defined data type. | 111| 13900042 | Unknown error. | 112 113**示例:** 114 115<!--code_no_check--> 116 ```ts 117 import { BusinessError } from '@kit.BasicServicesKit'; 118 import { common } from '@kit.AbilityKit'; 119 120 // 请在组件内获取context,确保this.getUIContext().getHostContext()返回结果为UIAbilityContext 121 let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 122 let path = context.filesDir; 123 statfs.getFreeSize(path, (err: BusinessError, number: number) => { 124 if (err) { 125 console.error("getFreeSize failed with error message: " + err.message + ", error code: " + err.code); 126 } else { 127 console.info("getFreeSize succeed, Size: " + number); 128 } 129 }); 130 ``` 131 132## statfs.getFreeSizeSync<sup>10+</sup> 133 134getFreeSizeSync(path:string): number 135 136以同步方法获取指定文件系统空闲字节数。 137 138**系统能力**:SystemCapability.FileManagement.File.FileIO 139 140**参数:** 141 142 | 参数名 | 类型 | 必填 | 说明 | 143 | ------ | ------ | ---- | ---------------------------- | 144 | path | string | 是 | 需要查询的文件系统的文件路径。 | 145 146**返回值:** 147 148 | 类型 | 说明 | 149 | --------------------- | -------------- | 150 | number | 返回空闲字节数。 | 151 152**错误码:** 153 154接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。 155 156| 错误码ID | 错误信息 | 157| -------- | -------- | 158| 13900002 | No such file or directory. | 159| 13900004 | Interrupted system call. | 160| 13900005 | I/O error. | 161| 13900008 | Bad file descriptor. | 162| 13900011 | Out of memory. | 163| 13900012 | Permission denied. | 164| 13900013 | Bad address. | 165| 13900018 | Not a directory. | 166| 13900030 | File name too long. | 167| 13900031 | Function not implemented. | 168| 13900033 | Too many symbolic links encountered. | 169| 13900038 | Value too large for defined data type. | 170| 13900042 | Unknown error. | 171 172**示例:** 173 174<!--code_no_check--> 175 ```ts 176 import { common } from '@kit.AbilityKit'; 177 178 // 请在组件内获取context,确保this.getUIContext().getHostContext()返回结果为UIAbilityContext 179 let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 180 let path = context.filesDir; 181 let number = statfs.getFreeSizeSync(path); 182 console.info("getFreeSizeSync succeed, Size: " + number); 183 ``` 184 185## statfs.getTotalSize 186 187getTotalSize(path: string): Promise<number> 188 189异步方法获取指定文件系统总字节数,以Promise形式返回结果。 190 191**系统能力**:SystemCapability.FileManagement.File.FileIO 192 193**参数:** 194 195 | 参数名 | 类型 | 必填 | 说明 | 196 | ---- | ------ | ---- | ---------------------------- | 197 | path | string | 是 | 需要查询的文件系统的文件路径。 | 198 199**返回值:** 200 201 | 类型 | 说明 | 202 | --------------------- | ------------ | 203 | Promise<number> | Promise对象,返回总字节数。 | 204 205**错误码:** 206 207接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。 208 209| 错误码ID | 错误信息 | 210| -------- | -------- | 211| 13900002 | No such file or directory. | 212| 13900004 | Interrupted system call. | 213| 13900005 | I/O error. | 214| 13900008 | Bad file descriptor. | 215| 13900011 | Out of memory. | 216| 13900012 | Permission denied. | 217| 13900013 | Bad address. | 218| 13900018 | Not a directory. | 219| 13900030 | File name too long. | 220| 13900031 | Function not implemented. | 221| 13900033 | Too many symbolic links encountered. | 222| 13900038 | Value too large for defined data type. | 223| 13900042 | Unknown error. | 224 225**示例:** 226 227<!--code_no_check--> 228 ```ts 229 import { BusinessError } from '@kit.BasicServicesKit'; 230 import { common } from '@kit.AbilityKit'; 231 232 // 请在组件内获取context,确保this.getUIContext().getHostContext()返回结果为UIAbilityContext 233 let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 234 let path = context.filesDir; 235 statfs.getTotalSize(path).then((number: number) => { 236 console.info("getTotalSize succeed, Size: " + number); 237 }).catch((err: BusinessError) => { 238 console.error("getTotalSize failed with error message: " + err.message + ", error code: " + err.code); 239 }); 240 ``` 241 242## statfs.getTotalSize 243 244getTotalSize(path: string, callback: AsyncCallback<number>): void 245 246异步方法获取指定文件系统总字节数,使用callback形式返回结果。 247 248**系统能力**:SystemCapability.FileManagement.File.FileIO 249 250**参数:** 251 252 | 参数名 | 类型 | 必填 | 说明 | 253 | -------- | --------------------------- | ---- | ---------------------------- | 254 | path | string | 是 | 需要查询的文件系统的文件路径。 | 255 | callback | AsyncCallback<number> | 是 | 异步获取总字节数之后的回调。 | 256 257**错误码:** 258 259接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。 260 261| 错误码ID | 错误信息 | 262| -------- | -------- | 263| 13900002 | No such file or directory. | 264| 13900004 | Interrupted system call. | 265| 13900005 | I/O error. | 266| 13900008 | Bad file descriptor. | 267| 13900011 | Out of memory. | 268| 13900012 | Permission denied. | 269| 13900013 | Bad address. | 270| 13900018 | Not a directory. | 271| 13900030 | File name too long. | 272| 13900031 | Function not implemented. | 273| 13900033 | Too many symbolic links encountered. | 274| 13900038 | Value too large for defined data type. | 275| 13900042 | Unknown error. | 276 277**示例:** 278 279<!--code_no_check--> 280 ```ts 281 import { BusinessError } from '@kit.BasicServicesKit'; 282 import { common } from '@kit.AbilityKit'; 283 284 // 请在组件内获取context,确保this.getUIContext().getHostContext()返回结果为UIAbilityContext 285 let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 286 let path = context.filesDir; 287 statfs.getTotalSize(path, (err: BusinessError, number: number) => { 288 if (err) { 289 console.error("getTotalSize failed with error message: " + err.message + ", error code: " + err.code); 290 } else { 291 console.info("getTotalSize succeed, Size: " + number); 292 } 293 }); 294 ``` 295 296## statfs.getTotalSizeSync<sup>10+</sup> 297 298getTotalSizeSync(path: string): number 299 300以同步方法获取指定文件系统总字节数。 301 302**系统能力**:SystemCapability.FileManagement.File.FileIO 303 304**参数:** 305 306 | 参数名 | 类型 | 必填 | 说明 | 307 | ---- | ------ | ---- | ---------------------------- | 308 | path | string | 是 | 需要查询的文件系统的文件路径。 | 309 310**返回值:** 311 312 | 类型 | 说明 | 313 | --------------------- | ------------ | 314 | number | 返回总字节数。 | 315 316**错误码:** 317 318接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。 319 320| 错误码ID | 错误信息 | 321| -------- | -------- | 322| 13900002 | No such file or directory. | 323| 13900004 | Interrupted system call. | 324| 13900005 | I/O error. | 325| 13900008 | Bad file descriptor. | 326| 13900011 | Out of memory. | 327| 13900012 | Permission denied. | 328| 13900013 | Bad address. | 329| 13900018 | Not a directory. | 330| 13900030 | File name too long. | 331| 13900031 | Function not implemented. | 332| 13900033 | Too many symbolic links encountered. | 333| 13900038 | Value too large for defined data type. | 334| 13900042 | Unknown error. | 335 336**示例:** 337 338<!--code_no_check--> 339 ```ts 340 import { common } from '@kit.AbilityKit'; 341 342 // 请在组件内获取context,确保this.getUIContext().getHostContext()返回结果为UIAbilityContext 343 let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 344 let path = context.filesDir; 345 let number = statfs.getTotalSizeSync(path); 346 console.info("getTotalSizeSync succeed, Size: " + number); 347 ``` 348