简体   繁体   中英

Node.js Problem While Reading File

I'm trying to do write text end of the file.And read it from the end.Or write it to end,then read it from there but when i try to write with r+ all text deleted.While I m reading it with a+ it does not read from the end or im making a mistake

var http = require('http');
var fs = require('fs');

var log = fs.createWriteStream('message.txt', {'flags': 'a'});
log.write('I want all of us money back');

var a = fs.createReadStream('message.txt', {'flags': 'a+',encoding:'utf8'});
a.on('data',function(data)
{
    console.log(data);
});

Is there a way to set it. I found it jquery get reverse but it's better to found a way from terminal.

It looks like what you are doing is the same thing as the functionality of the Stream.pipe function, so I suggest reading that documentation and using stream.pipe directly. However, I suspect the problem in your code is you are writing to the stream but then reading from it before the data that was written is actually flushed (meaning it is still being buffered by the OS).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM