Node.js 如何一次一行一行地读取文件

Node.js 如何一次一行一行地读取文件,node.js,Node.js,不是复制品 其他线程中的所有示例都回答了如何逐行读取文件。但是他们都没有关注如何一行一行地读取文件 为了说明这一点,我在另一个线程中修改了来自的代码: let index = 0; const rl = readline.createInterface({ input: fs.createReadStream(path.resolve(__dirname, './countries.txt')) }); rl.on('line', () => { console.log

不是复制品

其他线程中的所有示例都回答了如何逐行读取文件。但是他们都没有关注如何一行一行地读取文件

为了说明这一点,我在另一个线程中修改了来自的代码:

let index = 0;

const rl = readline.createInterface({
    input: fs.createReadStream(path.resolve(__dirname, './countries.txt'))
});

rl.on('line', () => {
    console.log('line', ++index);

    rl.pause();
});
根据执行代码的机器的速度,运行此程序的输出将大致如下:

line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
...
line 564
line 565
line 566
line 567
如何一行一行地读取文件


为了强调在示例中使用
rl.pause
的意图:我需要从文件中获取一行并停止进一步读取该文件,直到我明确请求第二行。

我真的不明白为什么要使用readline接口读取文件

我建议你使用,比如

你肯定会一次得到一条线


**为注释编辑。

我真的不明白为什么要使用readline界面来读取文件

我建议你使用,比如

你肯定会一次得到一条线


**为注释编辑。

以下是如何逐行读取文件,并在读取下一行之前处理一行

var fs=require('fs');
var readable = fs.createReadStream("data.txt", {
  encoding: 'utf8',
  fd: null
});
var lines=[];//this is array not a string!!!
readable.on('readable', function() {
  var chunk,tmp='';
  while (null !== (chunk = readable.read(1))) {
    if(chunk==='\n'){
      tmp='';
      lines.push(tmp);
    //  here tmp is containing a line you can process it right here.
    //  As i am just pushing the line in the array lines.
    //  use async if you are processing it asynchronously.
    }else
      tmp+=chunk;
  }
});
readable.on('end',function(){
  var i=0,len=lines.length;
  while(i<len)
    console.log(lines[i++]);
});
var fs=require('fs');
var readable=fs.createReadStream(“data.txt”{
编码:“utf8”,
fd:null
});
var行=[]//这是数组不是字符串!!!
readable.on('readable',function(){
变量组块,tmp='';
while(null!==(chunk=readable.read(1))){
如果(块=='\n'){
tmp='';
线推(tmp);
//这里tmp包含一行代码,您可以在这里处理它。
//因为我只是在推阵列线中的线。
//如果要异步处理,请使用async。
}否则
tmp+=chunk;
}
});
readable.on('end',function(){
变量i=0,len=lines.length;

while(i这里是如何逐行读取文件并在读取下一行之前处理一行

var fs=require('fs');
var readable = fs.createReadStream("data.txt", {
  encoding: 'utf8',
  fd: null
});
var lines=[];//this is array not a string!!!
readable.on('readable', function() {
  var chunk,tmp='';
  while (null !== (chunk = readable.read(1))) {
    if(chunk==='\n'){
      tmp='';
      lines.push(tmp);
    //  here tmp is containing a line you can process it right here.
    //  As i am just pushing the line in the array lines.
    //  use async if you are processing it asynchronously.
    }else
      tmp+=chunk;
  }
});
readable.on('end',function(){
  var i=0,len=lines.length;
  while(i<len)
    console.log(lines[i++]);
});
var fs=require('fs');
var readable=fs.createReadStream(“data.txt”{
编码:“utf8”,
fd:null
});
var lines=[];//这是数组而不是字符串!!!
readable.on('readable',function(){
变量组块,tmp='';
while(null!==(chunk=readable.read(1))){
如果(块=='\n'){
tmp='';
线推(tmp);
//这里tmp包含一行代码,您可以在这里处理它。
//因为我只是在推阵列线中的线。
//如果要异步处理,请使用async。
}否则
tmp+=chunk;
}
});
readable.on('end',function(){
变量i=0,len=lines.length;

while(我需要从文件中获取一行并停止进一步处理,直到我明确要求一行新行。如果需要,您可以暂停并恢复流。readline确实是根据文档逐行读取流……但它主要用于处理stdin,当用户键入某些内容并按[enter]时。请看,我需要从文件中获取一行,并停止进一步处理,直到我明确要求添加新行。如果需要,您可以暂停并恢复流。readline确实是根据文档逐行读取流……但它主要用于处理stdin,当用户键入内容并按[enter]时。看不到答案,但我发现它将接口抽象为逐行读取文件。不是答案,但我发现它将接口抽象为逐行读取文件。