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);
});

2 Answers 2

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}`);
});
Sign up to request clarification or add additional context in comments.

2 Comments

How do we know that procesing of lines is finished?
@MarinosAn, you need to add a callback function for the close event. rl.on('close', () => .........). See nodejs.org/api/readline.html#event-close
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);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.