【问题标题】:Read/Write File in a Nodejs在 Nodejs 中读/写文件
【发布时间】:2021-08-01 18:25:35
【问题描述】:

我有一个包含大约 3600 行的 Hex 文件。现在我在文件中有一个单词 1000 需要搜索并阅读该文本的确切上述行。例如:

1:2007200040040020A006000000050020A806000010050020B0060000101E0
2:2007400000020020C0060000EC010020C7060000F0010020CE060000F8010
3:04076000040200206F
4:20**1000**00B41200001810000015AB0110FDB301106DC00110FDAE011099B501
5:08102000000000002D0000009B
6:20102800B01200003C100000B1C00110A9BF011067C0011069B801
7:041048003000000074

现在在上述数据中,1000 位于第 4 行,并从该搜索操作中获取第 3 行数据。

我正在使用 nodejs FS 库。

以下是我目前的代码:

const parseTxt = async (csvFile) => {
    
    const data = await fs.readFileAsync(csvFile);
    const str = data.toString();
    const lines = str.split('\r\n');

    var total_lines = lines.length;
    console.log('Total Lines', total_lines);

    lines.map(line => {
        if(!line) {return null}
        result.push(Number(line))
    });

    lines.forEach(linebyline => {
        var search1000 = linebyline.substring(3, 7);
        if(search1000 == '1000'){
            // Here I want to check line number of this search and also above line data as well as line number of that above line.
            // Also want to Append "DOWNLOAD" text starting of this line data.
        }
    });

}

parseTxt('file.hex').then(() => {
    // console.log(mergeSort({arr: result, count: 0}));
    console.log('parseTxt===');
})

谢谢。

【问题讨论】:

标签: javascript node.js arrays express


【解决方案1】:
const fs = require('fs');
const readline = require('readline');

async function findPreviousLine(filename, searchString) {
    const fileStream = fs.createReadStream(filename);
  
    const rl = readline.createInterface({
        input: fileStream,
        crlfDelay: Infinity
    });

    let previousLine = '';
  
    for await (const line of rl) {
        if (line.includes(searchString)) {
            console.log(previousLine)
            return;
        }

        previousLine = line;
    }
}

findPreviousLine('data.txt', '1000');

查看answer中的代码,逐行阅读。

【讨论】:

    【解决方案2】:

    这是工作示例

    const fs = require('fs');
    const parseTxt = async (csvFile) => {
      let result=[];    
      const data = await fs.readFileSync(csvFile);
      const str = data.toString();
      const lines = str.split('\n');
      var total_lines = lines.length;
    
      lines.map(line => {
          if(!line) {return null}
          result.push(Number(line))
      });
    
      lines.forEach(linebyline => {
          var search1000 = linebyline.indexOf('1000');
          if(search1000 > -1){
              // Here I want to check line number of this search and also above line data as well as line number of that above line.
              // Also want to Append "DOWNLOAD" text starting of this line data.
          }
      });
    
    }
    
    parseTxt('file.hex').then(() => {
      // console.log(mergeSort({arr: result, count: 0}));
      console.log('parseTxt===');
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-12
      • 1970-01-01
      • 1970-01-01
      • 2015-02-01
      • 2021-12-12
      • 2012-10-08
      相关资源
      最近更新 更多