js-apis-statfs.md 4.4 KB
Newer Older
A
annie_wangli 已提交
1 2
# statfs

A
Annie_wang 已提交
3
> **NOTE:**<br>
A
annie_wangli 已提交
4
> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
A
annie_wangli 已提交
5

A
Annie_wang 已提交
6 7
This module provides information related to the file system. It provides JS APIs to obtain the total number of bytes and the number of idle bytes of the file system.

A
annie_wangli 已提交
8 9
## Modules to Import

A
annie_wangli 已提交
10
```js
A
annie_wangli 已提交
11
import statfs from '@ohos.statfs';
A
annie_wangli 已提交
12 13
```

A
annie_wangli 已提交
14
## Guidelines
A
annie_wangli 已提交
15

A
Annie_wang 已提交
16
Before using the APIs provided by this module to perform operations on a file or directory, obtain the path of the application sandbox. For details, see [getOrCreateLocalDir of the Context module](js-apis-Context.md).
A
annie_wangli 已提交
17

A
Annie_wang 已提交
18
Application sandbox path of a file or directory = Application directory + File name or directory name
A
annie_wangli 已提交
19

A
Annie_wang 已提交
20
For example, if the application directory obtained by using **getOrCreateLocalDir** is **dir** and the file name is **xxx.txt**, the application sandbox path of the file is as follows:
A
annie_wangli 已提交
21

A
annie_wangli 已提交
22
```js
A
annie_wangli 已提交
23
let path = dir + "xxx.txt";
A
annie_wangli 已提交
24 25 26 27 28 29
```

## statfs.getFreeBytes

getFreeBytes(path:string):Promise&lt;number&gt;

A
Annie_wang 已提交
30
Obtains the number of free bytes of the specified file system in asynchronous mode. This API uses a promise to return the result.
A
annie_wangli 已提交
31

A
annie_wangli 已提交
32 33 34
**System capability**: SystemCapability.FileManagement.File.FileIO

- **Parameters**
A
annie_wangli 已提交
35

A
annie_wangli 已提交
36
  | Name| Type  | Mandatory| Description                        |
A
annie_wangli 已提交
37
  | ------ | ------ | ---- | ---------------------------- |
A
annie_wangli 已提交
38
  | path   | string | Yes  | File path of the file system.|
A
annie_wangli 已提交
39

A
annie_wangli 已提交
40
- Return value
A
annie_wangli 已提交
41

A
annie_wangli 已提交
42
  | Type                 | Description          |
A
annie_wangli 已提交
43
  | --------------------- | -------------- |
A
annie_wangli 已提交
44
  | Promise&lt;number&gt; | Promise used to return the number of free bytes obtained.|
A
annie_wangli 已提交
45 46 47

- Example

A
annie_wangli 已提交
48
  ```js
A
Annie_wang 已提交
49
  let path = "/dev";
A
annie_wangli 已提交
50
  statfs.getFreeBytes(path).then(function (number){
A
Annie_wang 已提交
51
      console.info("getFreeBytes promise successfully:"+ number);
A
annie_wangli 已提交
52 53 54
  }).catch(function(err){
      console.info("getFreeBytes failed with error:"+ err);
  });
A
annie_wangli 已提交
55 56 57 58 59 60
  ```

## statfs.getFreeBytes

getFreeBytes(path:string, callback:AsyncCallback&lt;number&gt;): void

A
Annie_wang 已提交
61
Obtains the number of free bytes of the specified file system in asynchronous mode. This API uses a callback to return the result.
A
annie_wangli 已提交
62

A
annie_wangli 已提交
63
**System capability**: SystemCapability.FileManagement.File.FileIO
A
annie_wangli 已提交
64

A
annie_wangli 已提交
65 66 67
- **Parameters**

  | Name  | Type                       | Mandatory| Description                        |
A
annie_wangli 已提交
68
  | -------- | --------------------------- | ---- | ---------------------------- |
A
annie_wangli 已提交
69 70
  | path     | string                      | Yes  | File path of the file system.|
  | callback | AsyncCallback&lt;number&gt; | Yes  | Callback invoked to return the number of free bytes obtained.|
A
annie_wangli 已提交
71 72 73

- Example

A
annie_wangli 已提交
74
  ```js
A
annie_wangli 已提交
75
  statfs.getFreeBytes(path, function(err, number){
A
Annie_wang 已提交
76
      console.info("getFreeBytes callback successfully:"+ number);
A
annie_wangli 已提交
77 78 79 80 81
  });
  ```

## statfs.getTotalBytes

A
annie_wangli 已提交
82
getTotalBytes(path: string): Promise&lt;number&gt;
A
annie_wangli 已提交
83

A
Annie_wang 已提交
84
Obtains the total number of bytes of the specified file system in asynchronous mode. This API uses a promise to return the result.
A
annie_wangli 已提交
85

A
annie_wangli 已提交
86 87 88
**System capability**: SystemCapability.FileManagement.File.FileIO

- **Parameters**
A
annie_wangli 已提交
89

A
annie_wangli 已提交
90
  | Name| Type  | Mandatory| Description                        |
A
annie_wangli 已提交
91
  | ---- | ------ | ---- | ---------------------------- |
A
annie_wangli 已提交
92
  | path | string | Yes  | File path of the file system.|
A
annie_wangli 已提交
93

A
annie_wangli 已提交
94
- Return value
A
annie_wangli 已提交
95

A
annie_wangli 已提交
96
  | Type                 | Description        |
A
annie_wangli 已提交
97
  | --------------------- | ------------ |
A
annie_wangli 已提交
98
  | Promise&lt;number&gt; | Promise used to return the total number of bytes obtained.|
A
annie_wangli 已提交
99 100 101

- Example

A
annie_wangli 已提交
102
  ```js
A
Annie_wang 已提交
103
  let path = "/dev";
A
annie_wangli 已提交
104
  statfs.getTotalBytes(path).then(function (number){
A
Annie_wang 已提交
105
      console.info("getTotalBytes promise successfully:"+ number);
A
annie_wangli 已提交
106 107 108
  }).catch(function(err){
      console.info("getTotalBytes failed with error:"+ err);
  });
A
annie_wangli 已提交
109 110 111 112
  ```

## statfs.getTotalBytes

A
annie_wangli 已提交
113
getTotalBytes(path: string, callback: AsyncCallback&lt;number&gt;): void
A
annie_wangli 已提交
114

A
Annie_wang 已提交
115
Obtains the total number of bytes of the specified file system in asynchronous mode. This API uses a callback to return the result.
A
annie_wangli 已提交
116

A
annie_wangli 已提交
117 118 119
**System capability**: SystemCapability.FileManagement.File.FileIO

- **Parameters**
A
annie_wangli 已提交
120

A
annie_wangli 已提交
121
  | Name  | Type                       | Mandatory| Description                        |
A
annie_wangli 已提交
122
  | -------- | --------------------------- | ---- | ---------------------------- |
A
annie_wangli 已提交
123 124
  | path     | string                      | Yes  | File path of the file system.|
  | callback | AsyncCallback&lt;number&gt; | Yes  | Callback invoked to return the total number of bytes obtained.  |
A
annie_wangli 已提交
125 126 127

- Example

A
annie_wangli 已提交
128
  ```js
A
annie_wangli 已提交
129
  statfs.getTotalBytes(path, function(err, number){
A
Annie_wang 已提交
130
      console.info("getTotalBytes callback successfully:"+ number);
A
annie_wangli 已提交
131 132
  });
  ```