Knowledge is infinite

89 阅读1分钟
```// 引入
const fs = require('fs')
const path = require('path')
// 获取文件的绝对路径
const filePath = path.join(__dirname, 'dd', 'data.json')
console.log(filePath)
//读取内容
const content = fs.readFileSync(filePath, 'utf8')
console.log(content)
//转格式=>数组
let arr = JSON.parse(content)
// 把要添加的内容加入到数组中
arr.push({ "name": "西凉-马超" })
// 把数组 转为json类型
const newArrStr = JSON.stringify(arr)
//覆盖写入
fs.writeFileSync(filePath, newArrStr)