当前位置:网站首页>node:文件写入数据(readFile、writeFile),覆盖与增量两种模式
node:文件写入数据(readFile、writeFile),覆盖与增量两种模式
2022-07-29 08:16:00 【闲鱼_JavaScript】
介绍
- 使用 node 的 fs 模块中异步 writeFile方法;或者同步 writeFileSync方法;
- 写入文件(异步的fs.writeFile和同步的fs.writeFileSync)
file - 文件名或文件描述符。fs.writeFile(file, data[, options], callback)
data - 要写入文件的数据,可以是 String(字符串) 或 Buffer(缓冲) 对象。
options - 该参数是一个对象,包含 {encoding, mode, flag}。默认编码为 utf8, 模式为 0666 , flag 为 ‘w’
callback - 回调函数,回调函数只包含错误信息参数(err),在写入失败时返回。 - 读取文件(异步的 fs.readFile() 和同步的 fs.readFileSync())
fs.readFile(filename[, options], callback)
使用(以异步举例,同步则是以一个变量接收且无回调函数)
覆盖写入;(执行一次函数则文件内容置空,然后再写入)
const fs = require('fs'); const data = { name: 'zs' }; const wirte = (data) => { return data }; fs.writeFile("./end.js", `const a = ${ JSON.stringify(wirte (data))}`, err => { if (err) console.log('文件写入失败!' + err.message) console.log(`文件写入成功!:${ __dirname}\\end.js`) })

增量模式写入(保留之前的数据继续写入)
增量模式需要使用到fs的读取文件(fs.readFile);读取完毕后再次拼接写入;const fs = require('fs'); const data = { name: 'zs' }; const wirte = (data) => { return data } fs.readFile('./end.js', 'utf8', function(err, info) { if (err) console.log('文件读取失败!' + err.message) console.log(`文件读取成功!`,info) fs.writeFile("./end.js", `${ info};const time${ new Date().getTime()} = ${ JSON.stringify(wirte(data))}`, err => { if (err) console.log('文件写入失败!' + err.message) console.log(`文件写入成功!${ __dirname}\\end.js`) }) });
参考资料
- 菜鸟教程(node中文版官网需要VIP-199RMB,贫穷的我选择放弃)
边栏推荐
- Four pin OLED display based on stm32
- Process and concept of process
- Character shader exercise
- Alibaba political commissar system - Chapter 4: political commissars are built on companies
- 【NOI模拟赛】计算几何(凸包,暴力,并查集)
- 网络安全之安全基线
- Proteus simulation based on msp430f2491 (realize water lamp)
- An optimal buffer management scheme with dynamic thresholds paper summary
- Pnpm install appears: err_ PNPM_ PEER_ DEP_ ISSUES Unmet peer dependencies
- New energy shared charging pile management and operation platform
猜你喜欢

Ws2812b color lamp driver based on f407zgt6

Privacy is more secure in the era of digital RMB

MySQL rownum implementation

Inclination sensor accuracy calibration test

Inclination monitoring solution of Internet of things

Pnpm install appears: err_ PNPM_ PEER_ DEP_ ISSUES Unmet peer dependencies

TCP——滑动窗口

Simplefoc parameter adjustment 1-torque control

Detailed explanation of two modes of FTP

Dynamic thresholds buffer management in a shared buffer packet switch paper summary
随机推荐
Implementation of simple cubecap+fresnel shader in unity
Dp4301-sub-1g highly integrated wireless transceiver chip
Multifunctional signal generator based on AD9850
ML.NET相关资源整理
[beauty of software engineering - column notes] 29 | automated testing: how to kill bugs in the cradle?
Taiyuan bus route crawling
Arduino uno error analysis avrdude: stk500_ recv(): programmer is not responding
Usage of torch.tensor.to
Simple calculator wechat applet project source code
Time function in MySQL
Phy6252 is an ultra-low power Bluetooth wireless communication chip for the Internet of things
Explanation and closing method of server 135, 137, 138, 139, 445 and other ports
[beauty of software engineering - column notes] "one question and one answer" issue 2 | 30 common software development problem-solving strategies
110道 MySQL面试题及答案 (持续更新)
数仓分层设计及数据同步问题,,220728,,,,
Rotation in model space and rotation in world space
Qt/PyQt 窗口类型与窗口标志
BiSeNet v2
Exercise: store department information
[beauty of software engineering - column notes] "one question and one answer" issue 3 | 18 common software development problem-solving strategies