
nodejs如何读取/修改文件,下面web建站小编给大家详细介绍一下具体代码!
const fs = require('fs')
const path = require('path')
fs.readFile(path.join(__dirname, 'files', 'file1'), (err, buf) => {
if (err) throw err
console.log(buf.toString())
})
修改文件
const fs = require('fs')
const path = require('path')
fs.writeFile(path.join(__dirname, 'files', 'file3'), '欢迎光临', (err) => {
if (err) throw err
console.log('写入完成')
// 在文件末尾追加
fs.appendFile(path.join(__dirname, 'files', 'file3'), '\n\n男宾三位', (err) => {
if (err) throw err
console.log('追加完成')
// 修改文件名称
fs.rename(path.join(__dirname, 'files', 'file3'), path.join(__dirname, 'files', 'file3-rename'), (err) => {
if (err) throw err
console.log('改名完成')
})
})
})
上面是“nodejs如何读取/修改文件”的全面内容,想了解更多关于 nodejs 内容,请继续关注web建站教程。
当前网址:https://m.ipkd.cn/webs_3148.html
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

小墨鹰编辑器:微信生态AI排版运营一站式工具,支持Word/Excel/PPT/PDF导入
LFM2-24B-A2B模型使用入口,LiquidAI正式发布的LFM2家族模型
对一个首页前三的企业站的seo诊断分析
Endex
Ai好记:一款AI音视频转录与总结在线工具