I want to pipe content to file. Here's what I've wrote:
我想将内容传递给文件。这是我写的:
var fs = require('fs');
var Readable = require('stream').Readable;
var ws = fs.createWriteStream('xfiles.txt');
var rs = new Readable;
ws.on('finish', function() {
console.log('Written ' + ws.bytesWritten + ' ' + ws.path);
});
rs.push('foo bar baz');
rs.push('foo bar baz');
rs.push('foo bar baz');
rs.push(null);
rs.pipe(ws);
ws.end();
I expected the xfiles.txt to have 3 lines of 'foo bar baz'. But the output tells me that 0 bytes written, and when I check xfiles.txt it's empty.
我希望xfiles.txt有3行'foo bar baz'。但输出告诉我写入0字节,当我检查xfiles.txt时它是空的。
What do I miss?
我错过了什么?
EDIT
Sorry, found the answer right after posting this question. The answer in this SO question solved my problem.
对不起,在发布此问题后立即找到答案。这个问题的答案解决了我的问题。
1 个解决方案
#1
0
Found the answer just after posting this question. This SO question have the answer that also solved my problem.
在发布此问题后立即找到答案。这个问题的答案也解决了我的问题。
What I have to do is to call ws.end()
after the stream completes its async operation. So I have to put it inside a callback:
我要做的是在流完成其异步操作后调用ws.end()。所以我必须把它放在一个回调中:
ws.on('end', function() {
ws.end();
});
#1
0
Found the answer just after posting this question. This SO question have the answer that also solved my problem.
在发布此问题后立即找到答案。这个问题的答案也解决了我的问题。
What I have to do is to call ws.end()
after the stream completes its async operation. So I have to put it inside a callback:
我要做的是在流完成其异步操作后调用ws.end()。所以我必须把它放在一个回调中:
ws.on('end', function() {
ws.end();
});