[Node.js] Write or Append to a File in Node.js with fs.writeFile and fs.writeFileSync
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的更多相关文章
- [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 ...
- node.js fs.open 和 fs.write 读取文件和改写文件
Node.js的文件系统的Api //公共引用 var fs = require('fs'), path = require('path'); 1.读取文件readFile函数 //readFile( ...
- 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 ...
- 开始学习node.js了,第一节,fs文件系统 【fs.rename】重命名文件/文件夹
var fs=require('fs');fs.rename('c:\\a','c:\\a2',function(err){ if(err) console.log('error:'+err);}); ...
- Node.js(window)基础(2)——node环境下的模块,模块间调用
参考:http://www.liaoxuefeng.com/wiki/001434446689867b27157e896e74d51a89c25cc8b43bdb3000/00143450241959 ...
- Node.js学习笔记2(安装和配置Node.js)
1.安装 windows下安装,在http://nodejs.org下载安装包进行安装即可. linux下安装,使用yum或者下载源码进行编译. ...
- 【JS】 伪主动触发input:file的click事件
大家用到input:file标签时,对于input:file的样式难看的处理方法一般有2种: 采用透明化input:file标签的方法,上面放的input:file标签,下面放的是其他标签,实际点击的 ...
- 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 ...
- mkdir: Cannot create directory /file. Name node is in safe mode.
刚刚在hadoop想创建一个目录的时候,发现报错了 具体信息如下: [hadoop@mini1 hadoop-2.6.4]$ hadoop fs -mkdir /file mkdir: Cannot ...
随机推荐
- 关于MessageBox返回值
风格设置MB_OK. 此时无论点击确定还是点击X,都返回IDOK.风格设置MB_OKCANCEL,点击确认返回IDOK,点击取消和X都返回IDCANCEL.风格设置MB_YESNO,点击是返回IDYE ...
- 暑假集训 || LCA && RMQ
LCA定义为对于一颗树 树上两个点的最近公共祖先 一.Tarjan求LCA(离线方法 https://blog.csdn.net/lw277232240/article/details/7701751 ...
- Opencv竟然有中文资料
最近对于OpenCV看的较多,竟然不知不觉找到了一个中文网站,对于母语真的桥开心的嘻嘻 直方图均衡化: http://www.opencv.org.cn/opencvdoc/2.3.2/html/do ...
- Xcode导入第三方库
Xcode导入第三方库,例如TapkuLibrary iOS开源框架Tapku下载地址:https://github.com/devinross/tapkulibrary.git 1.创建你的工程项目 ...
- 「 SPOJ GSS3 」 Can you answer these queries III
# 题目大意 GSS3 - Can you answer these queries III 需要你维护一种数据结构,支持两种操作: 单点修改 求一个区间的最大子段和 # 解题思路 一个区间的最大子段 ...
- POJ-1328-放置雷达
这是一道贪心的题目,首先我们要知道,我们放置雷达的话我们可以做一个转换,就是已知岛屿的点坐标的时候,我们可以算一下,这个点以d为半径与x轴交点之间的线段在x轴上的投影,然后我们只需要在这个投影范围内设 ...
- ssh服务介绍
基本介绍 ssh:安全的远程登陆 要有客户端与服务器端,客户端主动链接服务端,那么服务端地址是不能变的. socket:套接字 标识应用唯一的地址 tcp/udp port端口号 cat /etc/s ...
- oracle函数笔记(1)
1.在数据库中显示当前时间:函数(sysdate) select sysdate from dual: 2.数据库中显示 :年/月/日 时/分/秒 ---函数:to_char(字段,'yyyy-mm- ...
- 条款9:绝不在构造和析构过程中调用virtual函数(Never call virtual functions during construction or destruction)
NOTE:在构造和析构期间不要调用virtual函数,因为这类调用从不下降至derived class(比起当前执行构造函数和析构函数的那层)
- 开发语言之---Python
Python,如果你想进军AI,或是不想被自动化运维淘汰,Python是一门必须课. 在未来的大学课堂上,也许也会将Python加入必修中,就像Java一样. Python之“Hello World” ...