我正在尝试使用节点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"
*/
};
};
}
});
});