fs-读取文件

var fs = require('fs');
 
// 同步 读取文件内容
// var conent = fs.readFileSync('hello.txt',{flag:'r',encoding:'utf-8'})
// console.log(123);
// console.log(conent);

// 异步
// fs.readFile("hello.txt",{flag:"r",encoding:"utf-8"},function(err,data){
//   if(err){
//     console.log(err);
//   }else{
//     console.log(data);
//   }
//   console.log(456);
// })

// 封装 异步读取文件
function fsRead(path){
  return new Promise(function(resolve,reject){
    fs.readFile(path,{flag:"r",encoding:"utf-8"},function(err,data){
      if(err){
        reject(err);
      }else{
        resolve(data);
      } 
    }) 
  }) 
}

// var w1  = fsRead('hello.txt')
// w1.then(function(res){
//   console.log(res);
// })

 async function ReadList(){
  var file2 = await fsRead('hello.txt');
  var file3 = await fsRead( file2 + '.txt');
  var fileContent  = await fsRead(file3 + '.txt');
  console.log(fileContent);
 }

 ReadList()
posted @ 2021-06-14 17:00  13522679763-任国强  阅读(379)  评论(0)    收藏  举报