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

原文地址:https://www.cnblogs.com/Answer1215/p/9823927.html

时间: 2024-10-29 04:31:04

[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 plain text, utf8 file - but you can also use fs.readFile to read a binary file as a buffer. We'll look at the differences between readFile and readFi

[JS进阶] HTML5 之文件操作(file)

前言 在 HTML 文档中 <input type="file"> 标签每出现一次,一个 FileUpload 对象就会被创建. 该元素包含一个文本输入字段,用来输入文件名,还有一个按钮,用来打开文件选择对话框以便图形化选择文件. 该元素的 value 属性保存了用户指定的文件的名称,但是当包含一个 file-upload 元素的表单被提交的时候,浏览器会向服务器发送选中的文件的内容而不仅仅是发送文件名. 当用户选择或编辑一个文件名,file-upload 元素触发 onc

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

参考:http://www.liaoxuefeng.com/wiki/001434446689867b27157e896e74d51a89c25cc8b43bdb3000/001434502419592fd80bbb0613a42118ccab9435af408fd000 1.node环境下的模块(module)概念:一个name.js文件就是一个模块,name是模块名称,模块中的函数方法称之为变量. 2.模块变量的调用:A.js中输出变量module.exports=函数名称; B.js引入模

Node.js学习笔记【1】入门(服务器JS、函数式编程、阻塞与非阻塞、回调、事件、内部和外部模块)

笔记来自<Node入门>@2011 Manuel Kiessling JavaScript与Node.js Node.js事实上既是一个运行时环境,同时又是一个库. 使用Node.js时,我们不仅仅在实现一个应用,同时还实现了整个HTTP服务器. 一个基础的HTTP服务器 server.js:一个可以工作的HTTP服务器 var http = require("http"); http.createServer(function(request, response) { r

Practical Node.js (2018版) 第7章:Boosting Node.js and Mongoose

参考:(mongoose和mongodb模块的区别)https://www.jianshu.com/p/87bcf4fdb479 参考: mongoose官网(https://mongoosejs.com/docs/models.html) 参考: 英文:Boosting Node.js和MongoDB with Mongoose 简介:mongoose Mongoose is a fully developed object document mapping (ODM) library for

Information centric network (icn) node based on switch and network process using the node

The present invention relates to an apparatus for supporting information centric networking. An information centric network (ICN) node based on a switch according to the present invention includes an ICN process configured to request information for

WinForm程序执行JS代码的多种方法以及使用WebBrowser与JS交互

原文地址:http://www.cnblogs.com/feiyuhuo/p/5474790.html 方法一 使用微软官方组件Interop.MSScriptControl 1.msscript.ocx下载的地址   http://www.microsoft.com/downloads/details.aspx?displaylang=zh-cn&FamilyID=D05FCF37-4D9F-4769-9442-0BCEEF907033 或者点击此处下载 Interop.MSScriptCon

js分析 汽_车_之_家 js生成css伪元素 hs_kw44_configUS::before

0.参考 https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle 0.1 Use with pseudo-elements getComputedStyle can pull style info from pseudo-elements (for example, ::after, ::before, ::marker, ::line-marker-see spec here). <style> h3:

js实现购物车数量的增加与减少,js实现购物车数量的自增与自减

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-