[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 ...
随机推荐
- 如何安装多个pip包
要在命令行上安装多个软件包,只需将它们作为以空格分隔的列表传递,例如: pip install wsgiref boto
- js数字转金额,ajax调用接口,后台返回html(完整页面),打开新窗口并写入html
一.转换成金额形式 function toMoney(num){ if(num){ if(isNaN(num)) { alert("金额中含有不能识别的字符"); return; ...
- postman使用--接口的关联
前戏 在实际接口测试过程中,接口经常会有关联,比如需要取上一个接口的返回值,然后作为参数传递给下一个接口作为参数,假设我们要获取A接口返回的userid值作为B接口的请求参数 先设置环境,所有接口在一 ...
- vue-loader 细节
vue-loader 能根据 .vue 文件,导入一个vue组件.我这里从 vue-cli 的构建项目中抽取了vue-loader 一个小例子出来:vuedemo/demo02 vue-loader ...
- 第一章:systemverilog简介
1.为何要学systemverilog ..... 2.systemverilog起源 ..... 3.systemverilog标准历程 systemverilog3.0 for 综合 system ...
- tornado框架基础01-路由简介
tornado 小而精 Django 大而全 Web框架 Tornado是一个由Python开发的Web框架 Web服务 利用Tornado,可以快速搭建和一个高性能的Web服务 非阻塞 Tornad ...
- 阿里云服务器ecs配置之安装mysql
安装mysql数据库 1.安装工作: 下载 mysql 源安装包 [root@ming ~]# wget http://dev.mysql.com/get/ ...
- 配置工程文件dll编译后copy路径
放到工程文件的最后面的配置节点: 下面的配置节点中生成路径换成实际的相对路径就可以了 修改:Prject.csproj 文件里面的配置节点 project配置节点里面的最后面 <Target ...
- luogu2633 Count on a tree
主席树放到树上而已 #include <algorithm> #include <iostream> #include <cstdio> using namespa ...
- mysql查最大字符串
select MAX(comp_code+0) from t_base_company 字符串 +0 把字符串转成数字