为什么我在node.js中收到此错误?这个想法是在完全读取一堆之后写一些东西,以避免在读取文件内容之前放入数据。因为他们要求更多细节,所以我在这里放了一些虚拟文本,以满足最小文本的要求。
> pi@raspberrypi:~/nodetest $ sudo node readstream.js
VAR1=2
VAR3=4
Fri Jan 06 2017 12:11:37 GMT+0000 (UTC)
Fri Jan 06 2017 12:33:14 GMT+0000 (UTC)
Fri Jan 06 2017 12:34:12 GMT+0000 (UTC)
appending
appending
appending
appending
appending
appending
appending
Fri Jan 06 2017 12:11:37 GMT+0000 (UTC)
Fri Jan 06 2017 12:33:14 GMT+0000 (UTC)
Fri Jan
Fri Jan 06 2017 12:11:37 GMT+0000 (UTC)
Fri Jan 06 2017 12:33:14 GMT+0000 (UTC)
Fri Jan
end
events.js:72
throw er; // Unhandled 'error' event
^
Error: EBADF, write
我的代码:
fs = require('fs');
path = 'config.txt';
readableStream = fs.createReadStream(path);
readableStream.setEncoding('utf8');
readableStream.once('data', function(data){ console.log(data); } );
readableStream.once('end', function(){
fs.open(path, 'a+', function(err, fd){
readStream = fs.createReadStream(null, {fd: fd, encoding: 'utf8', start: 14, end: 100});
readStream.once('data', function(data){ console.log(data);} );
readStream.once('data', console.log); // shorter
readStream.once('end', function(){
console.log('end');
try {
readStream.removeListener('data', function(data){} );
writeStream = fs.createWriteStream(null, {fd: fd, encoding: 'utf8', flags: 'a+'});
writeStream.write('appending\n'); // not working???
}
catch(err) {
console.log('error: [' + err.message + ']');
};
} );
} );
} );