简体   繁体   English

node.js修改文件数据流?

[英]node.js modify file data stream?

I need to copy one large data file to another destination with some modifications. 我需要将一个大型数据文件复制到另一个目的地并进行一些修改。 fs.readFile and fs.writeFile are very slow. fs.readFilefs.writeFile非常慢。 I need to read line by line, modify and write to new file. 我需要逐行阅读,修改并写入新文件。 I found something like this: 我找到了这样的东西:

fs.stat(sourceFile, function(err, stat){
    var filesize = stat.size;

    var readStream = fs.createReadStream(sourceFile);

    // HERE I want do some modifications with bytes

    readStream.pipe(fs.createWriteStream(destFile));
})

But how to make modifications ? 但是如何进行修改? I tried to get data with data event 我试图用data事件获取数据

readStream.on('data', function(buffer){
    var str = strToBytes(buffer);
    str.replace('hello', '');
    // How to write ???
});

but don't understand how to write it to file: 但不明白如何将其写入文件:

You should use transform stream and use pipes like this: 您应该使用transform流并使用这样的管道:

fs.createReadStream('input/file.txt')
     .pipe(new YourTransformStream())
     .pipe(fs.createWriteStream('output/file.txt'))

Then it's just a matter of implementing the transform stream as in this doc 然后,这只是在本文档实现转换流的问题

You can also make this easier for you using scramjet like this: 你也可以使用scramjet这样更容易:

fs.createReadStream('input/file.txt')
     .pipe(new StringStream('utf-8'))
     .split('\n')                                          // split every line
     .map(async (line) => await makeYourChangesTo(line))   // update the lines
     .join('\n')                                           // join again
     .pipe(fs.createWriteStream('output/file.txt'))

Which I suppose is easier than doing that manually. 我认为这比手动操作更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM