How can I write output to file? I tried instead of process.stdout
use fs.createWriteStream(temp + '/export2.json')
, but it's not working.
var rl = readline.createInterface({
input: fs.createReadStream(temp + '/export.json'),
output: process.stdout,
terminal: false
});
rl.on('line', function(line) {
rl.write(line);
});
Referring to node/readline
line 313:
Interface.prototype.write = function(d, key) {
if (this.paused) this.resume();
this.terminal ? this._ttyWrite(d, key) : this._normalWrite(d);
};
by calling rl.write()
you either write to tty or call _normalWrite()
whose definition follows the block.
Interface.prototype._normalWrite = function(b) {
// some code
// .......
if (newPartContainsEnding) {
this._sawReturn = /\r$/.test(string);
// got one or more newlines; process into "line" events
var lines = string.split(lineEnding);
// either '' or (concievably) the unfinished portion of the next line
string = lines.pop();
this._line_buffer = string;
lines.forEach(function(line) {
this._onLine(line);
}, this);
} else if (string) {
// no newlines this time, save what we have for next time
this._line_buffer = string;
}
};
Output is written into _line_buffer
.
line 96:
function onend() {
if (util.isString(self._line_buffer) && self._line_buffer.length > 0) {
self.emit('line', self._line_buffer);
}
self.close();
}
We find, _line_buffer
is emitted to line
event eventually. That's why you cannot write output to writeStream. To solve this problem, you can simply open a file using fs.openSync()
, and fs.write()
in rl.on('line', function(line){})
callback.
Sample code:
var rl = readline.createInterface({
input: fs.createReadStream(temp + '/export.json'),
output: process.stdout,
terminal: false
});
fd = fs.openSync('filename', 'w');
rl.on('line', function(line) {
fs.write(fd, line);
});
readline
does not write to file using with output
option. However you can create a write stream as normal and write to it as usual.
Example:
const { EOL } = require("os");
const rl = readline.createInterface({
input: fs.createReadStream(temp + '/export.json'),
});
const writeStream = fs.createWriteStream(temp + '/export2.json', { encoding: "utf8" })
rl.on('line', function(line) {
writeStream.write(`${line}${EOL}`);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With