(精华)2020年7月8日 Node.js 文件读取和写入

举报
愚公搬代码 发表于 2021/10/18 23:44:49 2021/10/18
【摘要】 读取 const fs = require('fs'); //同步读取 const data = fs.readFileSync('./package.json'); console.log('dat...

读取

const fs = require('fs');

//同步读取
const data = fs.readFileSync('./package.json');
console.log('data', data);
console.log('data2', data.toString());


//异步读取
const data2 = fs.readFile('./package.json', function (err, data) {
    console.log('异步data', data);
    console.log('异步data2', data.toString());
});

//利用promise ,第一种方式
// 异步函数promise化
// function promisify(fn) {
//     return function () {
//         let args = Array.prototype.slice.call(arguments);
//         return new Promise(function (resolve, reject) {
//             args.push(function (err, result) {
//                 if (err) reject(err);
//                 else resolve(result);
//             });
//             fn.apply(null, args);
//         });
//     }
// }
// var readFile = promisify(fs.readFile);
// readFile('./package.json').then((res)=>{
//     console.log(res.toString())
// })

// v >  8 , 第二种方式
const {
    promisify
} = require('util');

var readFile = promisify(fs.readFile);
// readFile('./package.json').then((res)=>{
//     console.log(res.toString())
// });
(async () => {
    // var data3 = await fs.readFile('./package.json'); //可以吗,不可以的,需要一个promise对象,
    var data3 = await readFile('./package.json');
    console.log('ppp', data3)
})();
// v> 10

const {readFile} = require('fs').promises;

readFile('./package.json').then((res)=>{
    console.log(res.toString())
});

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

写入

const fs = require("fs");

function get(key) {
  fs.readFile("./db.json", (err, data) => {
    const json = JSON.parse(data);
    console.log(json[key]);
  });
}
function set(key, value) {
  fs.readFile("./db.json", (err, data) => {
    // 可能是空文件,则设置为空对象
    const json = data ? JSON.parse(data) : {};
    json[key] = value; // 设置值
    // 重新写入文件
    fs.writeFile("./db.json", JSON.stringify(json), err => {
      if (err) {
        console.log(err);
      }
      console.log("写入成功!");
    });
  });
}

// 命令行接口部分
// require('readline') 模块提供了一个接口,用于从可读流(如 process.stdin)读取数据,
// 每次读取一行。 它可以通过以下方式使用:
const readline = require("readline");
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  prompt: '请输入>'
});
rl.prompt();
rl.on("line", function(input) {
  // get name 
  // set name kkkkkk
  // nm
  const [op, key, value] = input.split(" ");
 
  if (op === 'get') {
    get(key)
  } else if (op === 'set') {
    set(key, value)
  } else if(op === 'quit'){
    rl.close();
  }else {
    console.log('没有该操作');
  }
});

// 关闭readline.Interface实例,且撤回对input和output流的控制
rl.on("close", function() {
  console.log("程序结束");
  process.exit(0);
});


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56

文章来源: codeboy.blog.csdn.net,作者:愚公搬代码,版权归原作者所有,如需转载,请联系作者。

原文链接:codeboy.blog.csdn.net/article/details/107168729

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。