文件简单写入

 1 // fs 模块提供了用于与文件系统进行交互(以类似于标准 POSIX 函数的方式)的 API。
 2 
 3 
 4 
 5 //1. 引入 fs 模块
 6 const fs = require('fs');
 7 
 8 //2. 写入文件,没写入一次,覆盖之前的文件, 异步写入
 9 // fs.writeFile('test.log', 'hello Fs Module', function(err){
10 //     //判断错误  如果出现错误, err 保存的是错误对象, 没有出现错误, 则 err 是 null
11 //     if(err) {
12 //         console.log("写入失败");
13 //         return;
14 //     }
15 //     //
16 //     console.log('写入成功');
17 // });
18 
19 //追加写入
20 // fs.writeFile('test.log', 'hello Fs Module\r\n',{flag: 'a'}, function(err){
21 //     //判断错误  如果出现错误, err 保存的是错误对象, 没有出现错误, 则 err 是 null
22 //     if(err) {
23 //         console.log("写入失败");
24 //         return;
25 //     }
26 //     //
27 //     console.log('写入成功');
28 // });
29 
30 //同步写入
31 fs.writeFileSync('test.log', Date.now());// 645
32 console.log(Date.now());// 648
33 
34 
35 //练习 新建一个文件 『practiseFS.txt』, 向文件中写入内容, 『FS 真的很厉害哦!!』
36 // 需求 在 D:/a/b.txt  在b.txt中写入 『今天天气不错』
原文地址:https://www.cnblogs.com/fsg6/p/13081750.html