Readline output to file Node.js
Asked Answered
C

2

12

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);
});
Cassaundra answered 2/4, 2015 at 10:14 Comment(0)
C
8

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}`);
});
Cyprinid answered 21/12, 2020 at 12:58 Comment(2)
How do we know that procesing of lines is finished?Pb
@MarinosAn, you need to add a callback function for the close event. rl.on('close', () => .........). See nodejs.org/api/readline.html#event-closeMeddlesome
S
7

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);
});
Superman answered 2/4, 2015 at 20:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.