1 | # outputFile(file, data, [options, callback])
|
2 |
|
3 | Almost the same as `writeFile` (i.e. it [overwrites](http://pages.citebite.com/v2o5n8l2f5reb)), except that if the parent directory does not exist, it's created. `file` must be a file path (a buffer or a file descriptor is not allowed). `options` are what you'd pass to [`fs.writeFile()`](https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback).
|
4 |
|
5 | - `file` `<String>`
|
6 | - `data` `<String> | <Buffer> | <Uint8Array>`
|
7 | - `options` `<Object> | <String>`
|
8 | - `callback` `<Function>`
|
9 |
|
10 | ## Example:
|
11 |
|
12 | ```js
|
13 | const fs = require('fs-extra')
|
14 |
|
15 | const file = '/tmp/this/path/does/not/exist/file.txt'
|
16 |
|
17 | // With a callback:
|
18 | fs.outputFile(file, 'hello!', err => {
|
19 | console.log(err) // => null
|
20 |
|
21 | fs.readFile(file, 'utf8', (err, data) => {
|
22 | if (err) return console.error(err)
|
23 | console.log(data) // => hello!
|
24 | })
|
25 | })
|
26 |
|
27 | // With Promises:
|
28 | fs.outputFile(file, 'hello!')
|
29 | .then(() => fs.readFile(file, 'utf8'))
|
30 | .then(data => {
|
31 | console.log(data) // => hello!
|
32 | })
|
33 | .catch(err => {
|
34 | console.error(err)
|
35 | })
|
36 |
|
37 | // With async/await:
|
38 | async function example (f) {
|
39 | try {
|
40 | await fs.outputFile(f, 'hello!')
|
41 |
|
42 | const data = await fs.readFile(f, 'utf8')
|
43 |
|
44 | console.log(data) // => hello!
|
45 | } catch (err) {
|
46 | console.error(err)
|
47 | }
|
48 | }
|
49 |
|
50 | example(file)
|
51 | ```
|