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

A
annie_wangli 已提交
3 4
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE:**
> 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 6 7

## Modules to Import

A
annie_wangli 已提交
8
```js
A
annie_wangli 已提交
9
import statfs from '@ohos.statfs';
A
annie_wangli 已提交
10 11 12 13 14 15 16 17 18 19
```

## Note

Before using this module to perform operations on a file or directory, obtain the absolute path of the file or directory. For details, see [getOrCreateLocalDir of the Context module](js-apis-Context.md).

Absolute file or directory path = Application directory + File name or directory name

For example, if the application directory obtained by using **getOrCreateLocalDir** is **dir** and the file name is **xxx.txt**, the absolute path of the file is as follows:

A
annie_wangli 已提交
20
```js
A
annie_wangli 已提交
21
let path = dir + "xxx.txt";
A
annie_wangli 已提交
22 23
```

A
annie_wangli 已提交
24
## System Capabilities
A
annie_wangli 已提交
25

A
annie_wangli 已提交
26
SystemCapability.FileManagement.File.FileIO
A
annie_wangli 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

## statfs.getFreeBytes

getFreeBytes(path:string):Promise<number>

Obtains the number of free bytes of the specified file system in asynchronous mode. This method uses a promise to return the result.

- Parameters

  | Name| Type| Mandatory| Description|
  | ------ | ------ | ---- | ---------------------------- |
  | path   | string | Yes| File path of the file system.|

- Return values

  | Type| Description|
  | --------------------- | -------------- |
  | Promise<number> | Number of free bytes obtained.|

- Example

A
annie_wangli 已提交
48
  ```js
A
annie_wangli 已提交
49 50 51 52 53 54
  let path = "/data";
  statfs.getFreeBytes(path).then(function (number){
      console.info("getFreeBytes successfully:"+ number);
  }).catch(function(err){
      console.info("getFreeBytes failed with error:"+ err);
  });
A
annie_wangli 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
  ```

## statfs.getFreeBytes

getFreeBytes(path:string, callback:AsyncCallback<number>): void

Obtains the number of free bytes of the specified file system in asynchronous mode. This method uses a callback to return the result.

- Parameters

  | Name| Type| Mandatory| Description|
  | -------- | --------------------------- | ---- | ---------------------------- |
  | path     | string                      | Yes| File path of the file system.|
  | callback | AsyncCallback<number> | Yes| Callback invoked to return the number of free bytes obtained.|

- Example

A
annie_wangli 已提交
72
  ```js
A
annie_wangli 已提交
73
  statfs.getFreeBytes(path, function(err, number){
74
      // Do something.
A
annie_wangli 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
  });
  ```

## statfs.getTotalBytes

getTotalBytes.(path:string):Promise<number>

Obtains the total number of bytes of the specified file system in asynchronous mode. This method uses a promise to return the result.

- Parameters

  | Name| Type| Mandatory| Description|
  | ---- | ------ | ---- | ---------------------------- |
  | path | string | Yes| File path of the file system.|

- Return values

  | Type| Description|
  | --------------------- | ------------ |
  | Promise<number> | Total number of bytes obtained.|

- Example

A
annie_wangli 已提交
98
  ```js
A
annie_wangli 已提交
99 100 101 102 103 104
  let path = "/data";
  statfs.getTotalBytes(path).then(function (number){
      console.info("getTotalBytes successfully:"+ number);
  }).catch(function(err){
      console.info("getTotalBytes failed with error:"+ err);
  });
A
annie_wangli 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
  ```

## statfs.getTotalBytes

getTotalBytes(path:string, callback:AsyncCallback<number>): void

Obtains the total number of bytes of the specified file system in asynchronous mode. This method uses a callback to return the result.

- Parameters

  | Name| Type| Mandatory| Description|
  | -------- | --------------------------- | ---- | ---------------------------- |
  | path     | string                      | Yes| File path of the file system.|
  | callback | AsyncCallback<number> | Yes| Callback invoked to return the total number of bytes obtained.|

- Example

A
annie_wangli 已提交
122
  ```js
A
annie_wangli 已提交
123
  statfs.getTotalBytes(path, function(err, number){
124
      // Do something.
A
annie_wangli 已提交
125 126
  });
  ```