文件操作相关API
const fs = require('fs')
- fs.readFile(path[, options], callback) 读取文件
- path | | 文件路路径
- callback 回调函数
fs.readFile('./hello.txt','utf8',(err,data) => {
if(err) throw err;
console.log(data);
})
- s.writeFile(file, data[, options], callback) 写入文件
- file | | | 文件名或文件描述符。
- data | | | 写入的数据
- options |
- encoding | 写入字符串串的编码格式 默认值: ‘utf8’ 。
- mode 文件模式(权限) 默认值: 0o666 。
- flag 参阅⽀支持的文件系统标志。默认值: ‘w’ 。
- callback 回调函数
err
fs.writeFile('./hello.txt','this is a test',err => {
if(err) throw err;
console.log('写⼊入成功');
})
- fs.appendFile(path, data[, options], callback) 追加数据到文件