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")

[Node.js] Write or Append to a File in Node.js with fs.writeFile and fs.writeFileSync的更多相关文章

  1. [Node.js] Read a File in Node.js with fs.readFile and fs.readFileSync

    We'll read a csv file in node.js both synchronously, and asynchronously. The file we're reading is a ...

  2. node.js fs.open 和 fs.write 读取文件和改写文件

    Node.js的文件系统的Api //公共引用 var fs = require('fs'), path = require('path'); 1.读取文件readFile函数 //readFile( ...

  3. node Error: Could not locate the bindings file. Tried:解决

    问题描述: Error: Could not locate the bindings file. Tried: → C:\code\xxx\node_modules\deasync\build\dea ...

  4. 开始学习node.js了,第一节,fs文件系统 【fs.rename】重命名文件/文件夹

    var fs=require('fs');fs.rename('c:\\a','c:\\a2',function(err){ if(err) console.log('error:'+err);}); ...

  5. Node.js(window)基础(2)——node环境下的模块,模块间调用

    参考:http://www.liaoxuefeng.com/wiki/001434446689867b27157e896e74d51a89c25cc8b43bdb3000/00143450241959 ...

  6. Node.js学习笔记2(安装和配置Node.js)

            1.安装         windows下安装,在http://nodejs.org下载安装包进行安装即可.         linux下安装,使用yum或者下载源码进行编译.     ...

  7. 【JS】 伪主动触发input:file的click事件

    大家用到input:file标签时,对于input:file的样式难看的处理方法一般有2种: 采用透明化input:file标签的方法,上面放的input:file标签,下面放的是其他标签,实际点击的 ...

  8. android ReactNative之Cannot find entry file index.android.js in any of the roots

    android ReactNative之Cannot find entry file index.android.js in any of the roots 2018年04月02日 14:53:12 ...

  9. mkdir: Cannot create directory /file. Name node is in safe mode.

    刚刚在hadoop想创建一个目录的时候,发现报错了 具体信息如下: [hadoop@mini1 hadoop-2.6.4]$ hadoop fs -mkdir /file mkdir: Cannot ...

随机推荐

  1. 摄像头调用代码 笔记本的话,本身有一个摄像头,由于用的usb摄像头,需要把笔记本的摄像头禁用后,再使用

    摄像头调用代码 笔记本的话,本身有一个摄像头,由于用的usb摄像头,需要把笔记本的摄像头禁用后,再使用 <!DOCTYPE html> <html lang="en&quo ...

  2. COM技术开发(一)

    COM :基本的接口(IX,IY), 组件的实现(CA),以及对组件的调用 #include "pch.h" #include <iostream> #include ...

  3. postman使用--发送请求

    概述 上节讲了下接口的基础,从现在来学习怎么测接口.当然,测试接口有很多的工具,比如postman,jmeter等等,或者用代码测试,如果是做接口自动化我当然会选python,如果是调试接口,我特别喜 ...

  4. Android开发中JavaBean类和序列化知识的理解

    原创文章,转载请注明出处:http://www.cnblogs.com/baipengzhan/p/6296121.html Android开发中,我们经常用到JavaBean类以及序列化的知识,但经 ...

  5. Spoj8093 Sevenk Love Oimaster

    题目描述 题解: 对于所有n串建广义后缀自动机. (广义后缀自动机唯一区别就是每次将las附成1,并不需要在插入时特判) 建完后再建出parent树,然后用dfs序+树状数组搞区间不同种类. 其实就是 ...

  6. Mysql中max函数取得的值不是最大

    ①问题:遇到一个很有意思的问题,这里记录一下, 就是在使用max函数的时候发现取得的最大值其实不是最大值. 比如: 某一列中有10000000,和9999999, 其最大值应该是10000000但是查 ...

  7. 如何用纯 CSS 绘制一颗闪闪发光的璀璨钻石

    效果预览 按下右侧的"点击预览"按钮在当前页面预览,点击链接全屏预览. 在线演示 https://codepen.io/zhang-ou/pen/qYqwQp 可交互视频教程 此视 ...

  8. LeetCode(171) Excel Sheet Column Number

    题目 Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, re ...

  9. 【转】错误: ORA-01591: 锁被未决分布式事务处理 7.2.428982 持有--解决方案

    SQL 错误: ORA-01591: 锁被未决分布式事务处理 7.2.428982 持有 01591. 00000 -  "lock held by in-doubt distributed ...

  10. DataTable 转JSON数据

    /// <summary> /// 将datatable转换为json /// </summary> /// <param name="dtb"> ...