文件写入
语法示例如下
fs.writeFile(file, data[, options], callback)
fs.writeFileSync(file, data[, options])
let fs = require("fs");
let path = require("path");
// 1.拼接要写入的路径地址
let str = path.join(__dirname, "BNTang.txt");
// 2.写入数据
fs.writeFile(str,, "utf-8", function (err) {
if (err) {
throw new Error("写入数据失败");
} else {
console.log("写入数据成功");
}
})
let fs = require("fs");
let path = require("path");
// 1.拼接要写入的路径地址
let str = path.join(__dirname, "BNTang.txt");
// 2.写入数据
let buf = Buffer.from;
fs.writeFile(str, buf, "utf-8", function (err) {
if (err) {
throw new Error("写入数据失败");
} else {
console.log("写入数据成功");
}
});
let fs = require("fs");
let path = require("path");
// 1.拼接要写入的路径地址
let str = path.join(__dirname, "BNTang.txt");
// 2.写入数据
let res = fs.writeFileSync(str, , "utf-8");
// 如果 res 为 undefined 就说明成功了
console.log(res);