var fs = require("fs"); var data = ''; var readerStream = fs.createReadStream('input.txt'); readerStream.setEncoding('UTF-8'); readerStream.on('data', function(chunk) { data += chunk; }); readerStream.on('end', function(){ console.log(data); }); readerStream.on('error', function(err){ console.log(error.stack); }); console.log("execute end...");
一、假設當前目錄下有input.txt,內容爲:phoenixframework官網地址:www.cewan.lajavascript
二、以上代碼另存爲:readStream.js,在當前目錄執行:node readStream.js,效果以下:java