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