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  let context = getContext(this) as common.UIAbilityContext;
46  let path = context.filesDir;
47  statfs.getFreeSize(path).then((number: number) => {
48    console.info("getFreeSize succeed, Size: " + number);
49  }).catch((err: BusinessError) => {
50    console.error("getFreeSize failed with error message: " + err.message + ", error code: " + err.code);
51  });
52  ```
53
54## statfs.getFreeSize
55
56getFreeSize(path:string, callback:AsyncCallback<number>): void
57
58异步方法获取指定文件系统空闲字节数,使用callback形式返回结果。
59
60**系统能力**:SystemCapability.FileManagement.File.FileIO
61
62**参数:**
63
64  | 参数名   | 类型                        | 必填 | 说明                         |
65  | -------- | --------------------------- | ---- | ---------------------------- |
66  | path     | string                      | 是   | 需要查询的文件系统的文件路径。 |
67  | callback | AsyncCallback<number> | 是   | 异步获取空闲字节数之后的回调。 |
68
69**错误码:**
70
71接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
72
73**示例:**
74
75  ```ts
76  import { BusinessError } from '@kit.BasicServicesKit';
77  import { common } from '@kit.AbilityKit';
78
79  let context = getContext(this) as common.UIAbilityContext;
80  let path = context.filesDir;
81  statfs.getFreeSize(path, (err: BusinessError, number: number) => {
82    if (err) {
83      console.error("getFreeSize failed with error message: " + err.message + ", error code: " + err.code);
84    } else {
85      console.info("getFreeSize succeed, Size: " + number);
86    }
87  });
88  ```
89
90## statfs.getFreeSizeSync<sup>10+</sup>
91
92getFreeSizeSync(path:string): number
93
94以同步方法获取指定文件系统空闲字节数。
95
96**系统能力**:SystemCapability.FileManagement.File.FileIO
97
98**参数:**
99
100  | 参数名 | 类型   | 必填 | 说明                         |
101  | ------ | ------ | ---- | ---------------------------- |
102  | path   | string | 是   | 需要查询的文件系统的文件路径。 |
103
104**返回值:**
105
106  | 类型                  | 说明           |
107  | --------------------- | -------------- |
108  | number | 返回空闲字节数。 |
109
110**错误码:**
111
112接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
113
114**示例:**
115
116  ```ts
117  import { common } from '@kit.AbilityKit';
118
119  let context = getContext(this) as common.UIAbilityContext;
120  let path = context.filesDir;
121  let number = statfs.getFreeSizeSync(path);
122  console.info("getFreeSizeSync succeed, Size: " + number);
123  ```
124
125## statfs.getTotalSize
126
127getTotalSize(path: string): Promise&lt;number&gt;
128
129异步方法获取指定文件系统总字节数,以Promise形式返回结果。
130
131**系统能力**:SystemCapability.FileManagement.File.FileIO
132
133**参数:**
134
135  | 参数名 | 类型   | 必填 | 说明                         |
136  | ---- | ------ | ---- | ---------------------------- |
137  | path | string | 是   | 需要查询的文件系统的文件路径。 |
138
139**返回值:**
140
141  | 类型                  | 说明         |
142  | --------------------- | ------------ |
143  | Promise&lt;number&gt; | Promise对象,返回总字节数。 |
144
145**错误码:**
146
147接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
148
149**示例:**
150
151  ```ts
152  import { BusinessError } from '@kit.BasicServicesKit';
153  import { common } from '@kit.AbilityKit';
154
155  let context = getContext(this) as common.UIAbilityContext;
156  let path = context.filesDir;
157  statfs.getTotalSize(path).then((number: number) => {
158    console.info("getTotalSize succeed, Size: " + number);
159  }).catch((err: BusinessError) => {
160    console.error("getTotalSize failed with error message: " + err.message + ", error code: " + err.code);
161  });
162  ```
163
164## statfs.getTotalSize
165
166getTotalSize(path: string, callback: AsyncCallback&lt;number&gt;): void
167
168异步方法获取指定文件系统总字节数,使用callback形式返回结果。
169
170**系统能力**:SystemCapability.FileManagement.File.FileIO
171
172**参数:**
173
174  | 参数名   | 类型                        | 必填 | 说明                         |
175  | -------- | --------------------------- | ---- | ---------------------------- |
176  | path     | string                      | 是   | 需要查询的文件系统的文件路径。 |
177  | callback | AsyncCallback&lt;number&gt; | 是   | 异步获取总字节数之后的回调。   |
178
179**错误码:**
180
181接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
182
183**示例:**
184
185  ```ts
186  import { BusinessError } from '@kit.BasicServicesKit';
187  import { common } from '@kit.AbilityKit';
188
189  let context = getContext(this) as common.UIAbilityContext;
190  let path = context.filesDir;
191  statfs.getTotalSize(path, (err: BusinessError, number: number) => {
192    if (err) {
193      console.error("getTotalSize failed with error message: " + err.message + ", error code: " + err.code);
194    } else {
195      console.info("getTotalSize succeed, Size: " + number);
196    }
197  });
198  ```
199
200## statfs.getTotalSizeSync<sup>10+</sup>
201
202getTotalSizeSync(path: string): number
203
204以同步方法获取指定文件系统总字节数。
205
206**系统能力**:SystemCapability.FileManagement.File.FileIO
207
208**参数:**
209
210  | 参数名 | 类型   | 必填 | 说明                         |
211  | ---- | ------ | ---- | ---------------------------- |
212  | path | string | 是   | 需要查询的文件系统的文件路径。 |
213
214**返回值:**
215
216  | 类型                  | 说明         |
217  | --------------------- | ------------ |
218  | number | 返回总字节数。 |
219
220**错误码:**
221
222接口抛出错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
223
224**示例:**
225
226  ```ts
227  import { common } from '@kit.AbilityKit';
228
229  let context = getContext(this) as common.UIAbilityContext;
230  let path = context.filesDir;
231  let number = statfs.getTotalSizeSync(path);
232  console.info("getTotalSizeSync succeed, Size: " + number);
233  ```
234