更新时间:2023-02-05 09:46:37
您可以使用 Node 的内置 readline
模块.
You can use Node's built-in readline
module.
var fs = require('fs');
var readline = require('readline');
var fileCounter = -1;
var file = "foo.txt";
readline.createInterface({
input: fs.createReadStream(file),
terminal: false
}).on('line', function(line) {
var writable = fs.createWriteStream('data/part'+ fileCounter + '.txt', {flags:'w'});
writable.write(line);
fileCounter++
});
请注意,如果末尾没有换行符,这将丢失文件的最后一行,因此请确保最后一行数据后跟一个换行符.
Note that this will lose the last line of the file if there is no newline at the end, so make sure your last line of data is followed by a newline.
还要注意,文档表明它是稳定性指数 2,意思是:
Also note that the docs indicate that it is Stability index 2, meaning:
稳定性:2 - 不稳定 API 正在稳定中,但有还没有足够的真实世界测试被认为是稳定的.如果合理,将保持向后兼容性.
Stability: 2 - Unstable The API is in the process of settling, but has not yet had sufficient real-world testing to be considered stable. Backwards-compatibility will be maintained if reasonable.