示例:逐行读取文件流

一个常见的 readlineinput 选项情况是将文件系统的可读流传递给它。这是一个如何处理文件逐行解析的例子:

  1. const readline = require('readline');
  2. const fs = require('fs');
  3. const rl = readline.createInterface({
  4. input: fs.createReadStream('sample.txt')
  5. });
  6. rl.on('line', (line) => {
  7. console.log('Line from file:', line);
  8. });