You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 3 години
12345678910111213141516171819202122232425262728293031323334
  1. # outputFile(file, data, [options, callback])
  2. 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).
  3. - `file` `<String>`
  4. - `data` `<String> | <Buffer> | <Uint8Array>`
  5. - `options` `<Object> | <String>`
  6. - `callback` `<Function>`
  7. ## Example:
  8. ```js
  9. const fs = require('fs-extra')
  10. const file = '/tmp/this/path/does/not/exist/file.txt'
  11. fs.outputFile(file, 'hello!', err => {
  12. console.log(err) // => null
  13. fs.readFile(file, 'utf8', (err, data) => {
  14. if (err) return console.error(err)
  15. console.log(data) // => hello!
  16. })
  17. })
  18. // With Promises:
  19. fs.outputFile(file, 'hello!')
  20. .then(() => fs.readFile(file, 'utf8'))
  21. .then(data => {
  22. console.log(data) // => hello!
  23. })
  24. .catch(err => {
  25. console.error(err)
  26. })
  27. ```