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