Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing long strings to file (node js)

Tags:

node.js

I have a string which is 169 million chars long, which I need to write to a file and then read from another process.

I have read about WriteStream and ReadStream, but how do I write the string to a file when it has no method 'pipe'?

like image 720
Adrian E Avatar asked Dec 04 '22 03:12

Adrian E


1 Answers

Create a write stream is a good idea. You can use it like this:

var fs = require('fs');
var wstream = fs.createWriteStream('myOutput.txt');
wstream.write('Hello world!\n');
wstream.write('Another line\n');
wstream.end();

You can call to write as many time as you need, with parts of that 16 million chars string. Once you have finished writing the file, you can create a read stream to read chunks of the file.

However, 16 million chars are not that much, I would say you could read and write it at once and keep in memory the whole file.

Update: As requested in comment, I update with an example to pipe the stream to zip on-the-fly:

var zlib = require('zlib');
var gzip = zlib.createGzip();
var fs = require('fs');
var out = fs.createWriteStream('input.txt.gz');

gzip.pipe(out);

gzip.write('Hello world!\n');
gzip.write('Another line\n');

gzip.end();

This will create a gz file, and inside, only one file with same name (without the .gz at the end).

like image 190
greuze Avatar answered Dec 25 '22 03:12

greuze