2024年03月31日 建站教程
读取文件内容
const fs = require('fs'); fs.readFile('./test.txt', 'utf8', (err, data) => { if (err) throw err; console.log(data); });
替换文件内容
const fs = require('fs'); fs.readFile('./test.txt', 'utf8', (err, data) => { if (err) throw err; const replacedData = data.replace(/foo/g, 'bar'); console.log(replacedData); });
写入替换后的内容
const fs = require('fs'); fs.readFile('./test.txt', 'utf8', (err, data) => { if (err) throw err; const replacedData = data.replace(/foo/g, 'bar'); fs.writeFile('./test.txt', replacedData, 'utf8', (err) => { if (err) throw err; console.log('File saved'); }); });
本文链接:http://so.lmcjl.com/news/768/