18

我正在制作简单的 Node.js 应用程序,我需要删除文件中的第一行。请问有什么办法吗?我认为使用 fs.write 是可能的,但是如何呢?

4

3 回答 3

19

这是从文件中删除第一行的流式版本。
由于它使用流,意味着您不需要将整个文件加载到内存中,因此它更加高效和快速,并且可以处理非常大的文件而无需填充硬件内存。

var Transform = require('stream').Transform;
var util = require('util');


// Transform sctreamer to remove first line
function RemoveFirstLine(args) {
    if (! (this instanceof RemoveFirstLine)) {
        return new RemoveFirstLine(args);
    }
    Transform.call(this, args);
    this._buff = '';
    this._removed = false;
}
util.inherits(RemoveFirstLine, Transform);

RemoveFirstLine.prototype._transform = function(chunk, encoding, done) {
    if (this._removed) { // if already removed
        this.push(chunk); // just push through buffer
    } else {
        // collect string into buffer
        this._buff += chunk.toString();

        // check if string has newline symbol
        if (this._buff.indexOf('\n') !== -1) {
            // push to stream skipping first line
            this.push(this._buff.slice(this._buff.indexOf('\n') + 2));
            // clear string buffer
            this._buff = null;
            // mark as removed
            this._removed = true;
        }
    }
    done();
};

并像这样使用它:

var fs = require('fs');

var input = fs.createReadStream('test.txt'); // read file
var output = fs.createWriteStream('test_.txt'); // write file

input // take input
.pipe(RemoveFirstLine()) // pipe through line remover
.pipe(output); // save to file

另一种方式,不推荐。
如果您的文件不大,并且您不介意将它们加载到内存中,请加载文件、删除行、保存文件,但它速度较慢并且不适用于大文件。

var fs = require('fs');

var filePath = './test.txt'; // path to file

fs.readFile(filePath, function(err, data) { // read file to memory
    if (!err) {
        data = data.toString(); // stringify buffer
        var position = data.toString().indexOf('\n'); // find position of new line element
        if (position != -1) { // if new line element found
            data = data.substr(position + 1); // subtract string based on first line length

            fs.writeFile(filePath, data, function(err) { // write file
                if (err) { // if error, report
                    console.log (err);
                }
            });
        } else {
            console.log('no lines found');
        }
    } else {
        console.log(err);
    }
});
于 2013-06-28T13:11:11.847 回答
8

这是另一种方式:

const fs = require('fs');
const filePath = './table.csv';

let csvContent = fs.readFileSync(filePath).toString().split('\n'); // read file and convert to array by line break
csvContent.shift(); // remove the the first element from array
csvContent = csvContent.join('\n'); // convert array back to string

fs.writeFileSync(filePath, csvContent);
于 2020-02-25T23:53:06.830 回答
2

感谢@Lilleman 的评论,我对原始解决方案进行了修改,它需要“逐行”的第 3 方模块,并且可以在处理非常大的文件时防止内存溢出和竞速情况。

const fs = require('fs');
const LineReader = require('line-by-line');

const removeLines = function(srcPath, destPath, count, cb) {

  if(count <= 0) {
    return cb();
  }

  var reader = new LineReader(srcPath);
  var output = fs.createWriteStream(destPath);

  var linesRemoved = 0;
  var isFirstLine = true;

  reader.on('line', (line) => {
    if(linesRemoved < count) {
      linesRemoved++;
      return;
    }
    reader.pause();

    var newLine;
    if(isFirstLine) {
      newLine = line;
      isFirstLine = false;
    } else {
      newLine = '\n' + line;
    }

    output.write(newLine, () => {
      reader.resume();
    });
  })
  .on('error', (err) => {
    reader.pause();
    return cb(err);
  })
  .on('close', () => {
    return cb();
  })
}

----------------下面的原始解决方案---------------

受另一个答案的启发,这是一个修改后的流版本:

const fs = require('fs');
const readline = require('readline');

const removeFirstLine = function(srcPath, destPath, done) {

  var rl = readline.createInterface({
    input: fs.createReadStream(srcPath)
  });
  var output = fs.createWriteStream(destPath);
  var firstRemoved = false;

  rl.on('line', (line) => {
    if(!firstRemoved) {
      firstRemoved = true;
      return;
    }
    output.write(line + '\n');
  }).on('close', () => {
    return done();
  })
}

并且可以通过将“firstRemoved”更改为计数器来轻松修改以删除一定数量的行:

var linesRemoved = 0;
...
if(linesRemoved < LINES_TO_BE_REMOVED) {
  linesRemoved++;
  return;
}
...
于 2017-03-18T19:25:54.543 回答