Hughendman / Hughendman.github.io

2 stars 2 forks source link

node 和 文件(file) | YINXS的博客 #87

Open Hughendman opened 6 years ago

Hughendman commented 6 years ago

https://hughendman.github.io/post/7e3a5fc6.html

Hughendman commented 6 years ago
const readline = require('readline');
//Readline是Node.js里实现标准输入输出的封装好的模块,通过这个模块我们可以以逐行的方式读取数据流。使用require(“readline”)可以引用模块。
const fs = require('fs');
const r1 = readline.createInterface({
  input: fs.createReadStream("data.txt")
});
var i = 1; //txt中的行数
r1.on('line', function(line){ //事件监听
    console.log('Line from file:' + i + ":" + line);
  if(i == 1){
    console.log(line)
  }
  i+=1;
})