In node.js, you can require fs, and then call fs.writeFile with the filename, and data to write to that file (as a string or a buffer). That will overwrite the entire file, so to just append that data to the file instead, pass an options object with the flag key set to a. Or, you can use fs.appendFile. Make sure to handle errors using a callback as the last argument to writeFile and appendFile.

There are synchronous versions of each function as well, fs.writeFileSync and fs.appendFileSync, which will throw errors, instead of returning them in a callback.

const fs = require('fs')

const contents = 'Data to write 123\n'

// Write File, async:
fs.writeFile('output.txt', contents, {
// flag: 'a' // 'a' flag for append
}, (err) => {
console.log("ERROR: ", err)
}) // Append File, async:
fs.appendFile('output.txt', contents, (err) => {
console.log("ERROR: ", err)
}) // Write File, Sync:
fs.writeFileSync('output.txt', contents) // Append File, Sync:
fs.appendFileSync('output.txt', contents) // Sync Error example:
try {
fs.appendFileSync('output.txt', contents, {
flag: 'ax'
})
} catch(e) {
console.log("ERROR: ", e)
} console.log("\nEnd of script")

最新文章

  1. Nodejs reactjs服务端渲染优化SEO
  2. Java中对List集合内的元素进行顺序、倒序、随机排序的示例代码
  3. FusionCharts 相关知识
  4. javascrip 分享到
  5. 基于异步的MVC webAPI控制器
  6. Entity Framewor中的 Migration
  7. C# 利用ajax实现局部刷新
  8. TF-IDF学习(python实现)
  9. Vue.js2.0中的变化(持续更新中)
  10. C++面试基础之回调
  11. 极简】如何在服务器上安装SSL证书?
  12. 006-mac下finder操作
  13. Github安全整理(转载)
  14. vue 踩坑-事件修饰符
  15. Mac下配置Oracle数据库客户端远程连接数据库服务器
  16. log4j每天,每小时产生一日志文件
  17. Spring_AOP动态代理详解(转)
  18. Swift 无操作时自动登出
  19. Nginx概述、安装及配置详解
  20. 浅谈PCA

热门文章

  1. Microsoft Project 2010基础使用方法
  2. poptip 外面 放 input 使用 iview vue
  3. 数组、list排序
  4. PyTorch如何构建深度学习模型?
  5. canvas使用自定义字体没有效果
  6. 剑指Offer整理笔记
  7. POJ-1163 递推
  8. requests 模块笔记
  9. centos7 ftp 500 OOPS: cannot change directory:/var/ftp/xutong/
  10. 又见GCD (已知最大公约数和其中一个数求另一个数)