Last active
December 19, 2015 16:19
-
-
Save you21979/5983025 to your computer and use it in GitHub Desktop.
node.jsでファイルを一行づつ読み込む
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * file read line | |
| */ | |
| "use strict"; | |
| var fs = require("fs"); | |
| /** | |
| * node.js 0.8 or 0.10 setting | |
| */ | |
| var _setImmediate = (function(){ | |
| return (global.setImmediate ? setImmediate : | |
| (process.nextTick ? process.nextTick : | |
| function(cb){ setTimeout(cb, 0); } | |
| )); | |
| })(); | |
| // execCallback = function(line){} | |
| // resultCallback = function(err){} | |
| var fileReadLine = module.exports = function( filepath, execCallback, resultCallback ){ | |
| fs.open(filepath, 'r', function( err, fd ){ | |
| if( err ){ | |
| resultCallback(err); | |
| return; | |
| } | |
| var buffSize = 4096; | |
| var tmp = ""; | |
| var update = function(pos){ | |
| _setImmediate(function(){ | |
| fs.read(fd, buffSize, pos, "utf-8", function( err, readBuff, readBytes ){ | |
| if( err ){ | |
| fs.close(fd, function(){ | |
| resultCallback(err); | |
| }); | |
| return; | |
| } | |
| if( readBytes === 0 ){ | |
| fs.close(fd, function(){ | |
| resultCallback(null); | |
| }); | |
| }else{ | |
| tmp += readBuff; | |
| var lines = tmp.split("\n"); | |
| for(var i = 0; i<lines.length - 1; ++i){ | |
| execCallback(lines[i]); | |
| } | |
| if(lines.length > 1){ | |
| tmp = lines[lines.length - 1]; | |
| } | |
| update(pos + readBytes); | |
| } | |
| }); | |
| }); | |
| }; | |
| update(0); | |
| }); | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var fs = require('fs'), | |
| readline = require('readline'); | |
| var filepath = "textdata.log"; | |
| var rs = fs.ReadStream(filepath); | |
| var rl = readline.createInterface({'input': rs, 'output': {}}); | |
| var i = 0; | |
| rl.on('line', function (line) { | |
| console.log(i++ + ': ' + line.trim()); | |
| }); | |
| rl.on('close', function(){ | |
| console.log("end"); | |
| }); | |
| rl.resume(); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://superbrothers.hatenablog.com/entry/2012/12/11/003310
スマートなやり方があった