[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 ...
随机推荐
- C++ new delete(一)
在C#.Java這種managed語言,因為有garbage collection,所以完全不用考慮free()或delete,但在C/C++,有時候要delete的,有時又不用,到底哪些改delet ...
- vue-cli中添加使用less
在vue-cli中构建的项目是可以使用less的,但是查看package.json可以发现,并没有less相关的插件,所以我们需要自行安装. 第一步:安装 npm install less less- ...
- [题解] codevs 1486 愚蠢的矿工
http://codevs.cn/problem/1486/ 我们比较熟悉二叉树,题目中给出的是一棵多叉树,我们需要将这可二叉树改造成二叉树. 二叉树可以为这样的: 父亲结点左边储存儿子,右边储存兄弟 ...
- More Effective C++ - 章节二 : 操作符(operators)
5. 对定制的 "类型转换函数" 保持警觉 允许编译器执行隐式类型转换,害处多过好处,不要提供转换函数,除非你确定需要. class foo { foo(int a = 0, in ...
- POJ-1724 深搜剪枝
这道题目如果数据很小的话.我们通过这个dfs就可以完成深搜: void dfs(int s) { if (s==N) { minLen=min(minLen,totalLen); return ; } ...
- Python简介--备份
脚本开头指定解释器路径 /usr/bin/env python 和 /usr/bin/python比较.(Linux在PATH中找到第一个路径的Python版本来执行) PATH中第一个路径是 ...
- react深入 - 手写实现react-redux api
简介:简单实现react-redux基础api react-redux api回顾 <Provider store>把store放在context里,所有子组件可以直接拿到store数据 ...
- list、tuple、dict内部功能释义
一.list内部功能释义 运用频次:☆☆☆☆☆ 1. append():列表末尾插入元素 [示例] >>> L1=[11,22,33] >>> L1.append( ...
- hibernate中时间比较的小笔记
// 开单时间 if (!"".equals(startDate) && startDate != null) { queryCondition = queryCo ...
- 用python写自定义模板
模板语法有点像php !/usr/bin/env python """ #demo.py.html <html> <?py include head.p ...