标签:
?
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); ????} ? ); |
标签:
原文地址:http://www.cnblogs.com/time-is-life/p/5546638.html