写入文件内容writeFile()
writeFile(file, data[, options], callback)
file
写入文件路径包含文件名称(没有文件则会创建文件)data
写入的内容(覆盖原本内容)options
写入文件内容的编码格式(默认utf8)callback
回调函数err
错误信息对象null
```javascript import { writeFile } from “fs”;
let path = “./index.txt”; let content = “Hello 沐颖汐”;
writeFile(path, content, err => { console.log(err); });
<a name="dWT7f"></a>
### 判断写入文件是否成功
```javascript
import { writeFile } from "fs";
let path = "./index.txt";
let content = "Hello 沐颖汐";
writeFile(path, content, err => {
if (err) {
return console.log("写入文件失败:\n" + err.message);
}
console.log("写入文件成功\n");
});