读取时写入文件 - nodejs

时间:2018-06-11 21:02:18

标签: javascript node.js filesystems readline

我正在尝试使用节点readline模块读取文件。在阅读文件时,我正在检查特定关键字。当我遇到该关键字时,我想在文件中添加一个新行,就在遇到该关键字的行之后。这是我到目前为止所做的事情。

csvHandler().then(( scanData ) => {

    //scanData is an object from where I'll get the line to be added in the file.

    const rl = readline.createInterface({
        input: fs.createReadStream( 'test.mgf' )
    });

    let read = false;

    rl.on( 'line', ( line ) => {

        if(line.split(" ")[0] === "BEGIN" ){
            read = true;
        }

        if( line.split(" ")[0] === "END" ){
            read = false;
        }

        if( read ){
            if( line.toString().includes("Scan") ){
                var scanNumber = line.split(" ")[2];
                if( scanNumber in scanData ){
                    /*
                     Here I have encountered the element now I want to add a new line to the file right after this particular line and then set `read` to `false`, so that it doesn't read until it encounters next "Begin"
                    */
                };
            };
        }
    });
});

0 个答案:

没有答案