node.js读写文件操作
生活随笔
收集整理的這篇文章主要介紹了
node.js读写文件操作
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
讀取文件
//導(dǎo)入fs模塊 const fs = require('fs')//調(diào)用fs.readFile()方法讀取文件 fs.readFile('./files/1.txt','utf-8',function (err,dataStr) {console.log(err)console.log('--------------')console.log(dataStr)})寫入文件
//導(dǎo)入模塊 const fs = require('fs')//調(diào)用fs.writeFile()的語法寫入內(nèi)容 fs.writeFile('./1.txt','調(diào)用fs.writeFile()的語法寫入內(nèi)容','utf-8',function (err) {console.log(err) })路徑的拼接
//導(dǎo)入模塊 const path = require('path')//路徑的拼接 const pathStr = path.join('/a','/b/c','../c','./d','e') console.log(pathStr)const pathStr2 = path.join(__dirname,'./files/1.txt') console.log(pathStr2)總結(jié)
以上是生活随笔為你收集整理的node.js读写文件操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PHP 函数 - 返回值
- 下一篇: Wordpress 提速之 Gzip 压