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. python基础一 day2 字符串操作

    s.capitalize()  s.upper()  s.lower() s.swapcase()   s.title()  s.center(20,"#")   s.expand ...

  2. 查看cuda版本和cudann

    nvcc -V 没有找到直接查询cudann版本的命令,但发现cudann装在 /usr/local/cuda/lib64/目录下,libcudnn.so就是相应版本

  3. POJ 3620 Avoid The Lakes (求连接最长的线)(DFS)

    Description Farmer John's farm was flooded in the most recent storm, a fact only aggravated by the i ...

  4. Counting Kangaroos is Fun 求最少可见袋鼠数

    Description There are n kangaroos with pockets. Each kangaroo has a size (integer number). A kangaro ...

  5. 【MFC】利用MFC写一个计时器小程序

    1整体设计 创建对话框程序,并且设计对话框相关控件如图 相应的ID和对应的成员变量如图: 我的想法是这样的,只读属性的编辑框添加有CString类型的成员变量(如s_hour),在xxxDlg.h里另 ...

  6. Github 多账号配置

    1. 不同账户,生成不同密钥ssh-keygen -t rsa -f github1 -C "xxx@163.com"ssh-keygen -t rsa -f github2 -C ...

  7. Monkey King(左偏树)

    洛谷传送门 每次给出要争吵的猴子a和b,用并查集判断如果他们是朋友输出-1 如果不是,找出a,b在的堆的根A,B,分别合并A,B的左右孩子,再合并一下. 之后把A,B的数据更改一下:权值除以2,左右孩 ...

  8. 【ZJOI2017 Round1练习】

    喜闻乐见(爆蛋滚粗)的ZJOI模拟赛终于开始了 可以又一次感受被屠的快感 DAY1: T1:线段树打错-70 正解分块听卡常还要调块的大小 T2:数学弱爆 是道结论题 T3:暴力分滚粗 DAY2: T ...

  9. C++,C程序设计入门——《高质量程序设计第4章》

    1. 连接规范 1. extern “C” 2. 一部分采用C的连接规范 #ifdef __cplusplus extern "C" { #endif #ifdef __cplus ...

  10. msp430项目编程

    msp430中项目---LED数码管显示 1.数码管介绍 2.代码(直接使用引脚驱动) 3.代码(使用译码器驱动) 4.项目总结 msp430项目编程 msp430入门学习