Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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

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:

like image 666
Arti Avatar asked Aug 19 '17 19:08

Arti


People also ask

How to create streams in Node JS?

Step 1) Create a file called data.txt which has the below data. Let assume this file is stored on the D drive of our local machine. Tutorial on Node.js Step 2) Write the relevant code which will make use of streams to read data from the file. We first need to include the 'fs' modules which contain all the functionality required to create streams.

What is FILESTREAM in Node JS?

Filestream in Node.js Node makes extensive use of streams as a data transfer mechanism. For example, when you output anything to the console using the console.log function, you are actually using a stream to send the data to the console. Node.js also has the ability to stream data from files so that they can be read and written appropriately.

How to read data from a file in Node JS?

Step 1) Create a file called data.txt which has the below data. Let assume this file is stored on the D drive of our local machine. Tutorial on Node.js Step 2) Write the relevant code which will make use of streams to read data from the file.

What is a transform stream in Node JS?

Transform streams: a duplex stream in which the output (or writable stream) is dependent on the modification of the input (or readable stream). The file system module ( fs) is a native Node.js module for manipulating files and navigating the local file system in general. It provides several methods for doing this.


1 Answers

You should use transform stream and use pipes like this:

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:

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.

like image 199
Michał Karpacki Avatar answered Sep 29 '22 17:09

Michał Karpacki