const fs=require('fs');
//文件名,写的内容,编码风格,调函数(如果出现错误,就要抛出)
fs.writeFile('./text','this is a test',{
encoding:'utf8'
},(err)=>{
if(err) throw err;
console.log('done!');
})
//也可以这默写编码格式
fs.writeFile('./text','this is a test','utf8',(err)=>{
if(err) throw err;
console.log('done!');
})
这写入的是字符串,那么如果写buffer的话,也可以忽略编码风格这一参数
const content = Buffer.from('this is a tasr 文本');
fs.writeFile('./text',content,(err)=>{
if(err) throw err;
console.log('done!');
})