fs.createReadStream()
和fs.createWriteStream()
仅支持文件路径,但我需要从文件描述符读取(或写入)(传递给子进程/从子进程传递)。 / p>
注意我需要Streams,因此fs.open/fs.read/fs.write
是不够的。
答案 0 :(得分:14)
当您致电fs.createReadStream时,您可以传入文件描述符:
var fs = require('fs');
var fd = fs.openSync('/tmp/tmp.js', 'r');
var s = fs.createReadStream(null, {fd: fd});
s.pipe(process.stdout);
如果有fd
选项,则忽略文件名。
答案 1 :(得分:1)
// Open &3:
process.oob1 = fs.createWriteStream(null, { fd: 3 });
// Write to &3 / oob1 (out-of-band 1)
process.oob1.write("Note: this will throw an exception without 3>&1 or something else declaring the existence of &3");