// 导入fs模块
const fs = require('fs')
// 读写结合
const readFile = fs.readFileSync('./静夜思.txt')
fs.writeFileSync('./静夜思复制版.txt', readFile)
// 读写结合
const { promises: { readFile, writeFile } } = require('fs');
(async () => {
const readData = await readFile('./test.txt')
await writeFile('./木兰诗片段.txt', readData)
console.log('写入成功');
})()
// 读写结合流式操作
// 读取流
const rs = fs.createReadStream('./test.txt')
// 写入流
const ws = fs.createWriteStream('./木兰诗片段.txt')
rs.on('data', chunk => {
ws.write(chunk)
})
rs.on('end', () => {
console.log('写入完成');
})
// 读取流
const rs = fs.createReadStream('./test.txt')
// 写入流
const ws = fs.createWriteStream('./木兰诗片段.txt')
rs.pipe(ws)
【node】读写文件结合
最新推荐文章于 2024-10-31 15:49:24 发布