【问题标题】:How to begin reading a file using the readline module from line 2?如何使用第 2 行的 readline 模块开始读取文件?
【发布时间】:2020-09-25 19:50:45
【问题描述】:

如何从第 2 行读取文件?我陷入了第一行无关紧要的问题,我应该能够从文件的第二行读取?我该如何使用 readline 模块呢?

【问题讨论】:

    标签: javascript node.js file readline


    【解决方案1】:

    使用事件 line 捕获每个换行符: https://nodejs.org/api/readline.html#readline_event_line

    const fs = require('fs')
    const readline = require('readline')
    
    const file = readline.createInterface({
      input: fs.createReadStream('{your_file_path}')
    })
    
    let index = 1
    file.on('line', (line) => {
      if (index === 2) {
        console.log(line)
        // to what you want with data
      }
      index++
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多