码迷,mamicode.com
首页 > Web开发 > 详细

Note.js的stream用法一例

时间:2016-05-31 18:41:40      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

?

Note.js,用stream读取文件的内容,注意decoder的用法

const fs = require(‘fs‘);

?

var rr = fs.createReadStream(‘data\\foo.txt‘);

?

// Pull off a header delimited by \n\n

// use unshift() if we get too much

// Call the callback with (error, header, stream)

const StringDecoder = require(‘string_decoder‘).StringDecoder;

function parseText(stream, callback) {

stream.on(‘error‘, callback);

stream.on(‘readable‘, onReadable);

var decoder = new StringDecoder(‘utf8‘);

var header = ‘‘;

function onReadable() {

var chunk;

while (null !== (chunk = stream.read())) {

var str = decoder.write(chunk);

console.log(str);

}

}

}

?

parseText(rr,

????function(source, header, stream )

????{

????????console.write(header);

????}

?

);

Note.js的stream用法一例

标签:

原文地址:http://www.cnblogs.com/time-is-life/p/5546638.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!