我正在编写此应用程序,在这里我必须读取.txt文件并将其写入具有其他修改的另一个.txt文件中,但是我还必须删除已经立即读取的行,这就是问题所在。
我的想法是在每一行写一个空的长字符串,但它会附加在文件的底部。
const LineByLineReader = require('line-by-line'),
lr = new LineByLineReader('file.txt');
const writeStream = fs.createWriteStream('random.txt')
const clearLiner = fs.createWriteStream('file.txt', {
flags: 'a',
encoding: null,
mode: 0666
})
const pattern = 'isa'
let reverse = false;
lr.on('line', function(line){
lr.pause();
clearLiner.write(` \n`); // It starts at the bottom of the file
if(reverse){
line = line.split('')
line = line.reverse()
line = line.join('')
reverse = false;
}
setTimeout(function () {
if (line.includes(pattern)) {
reverse = true;
line.endsWith(pattern) ? reverse = false : null ;
}
writeStream.write(`${line} \n`)
lr.resume()
}, 1000)
})