02_08.同步读写文件.js

var fs = require("fs");

console.log("a文件开始读取");
var a = fs.readFileSync("./data/a.txt");
console.log("a文件读取完成");
console.log(a);

console.log("b文件开始读取");
//try和catch是用来防止代码出现问题导致后面代码无法运行的,
//也就是说如果以后出现同步的代码,为防止代码能够顺利运行,
//应该将自己不确定的代码放到try里面去运行,
//然后在catch里面书写假如失败的情况
try {
    var b = fs.readFileSync("./data");
    console.log("b文件读取完成");
    console.log(b);
}catch(e) {
    console.log("b文件读取失败");
}


console.log("c文件开始读取");
var c = fs.readFileSync("./data/c.txt");
console.log("c文件读取完成");
console.log(c);

你可能感兴趣的:(02_08.同步读写文件.js)