Skip to content

Instantly share code, notes, and snippets.

@you21979
Last active December 19, 2015 16:19
Show Gist options
  • Save you21979/5983025 to your computer and use it in GitHub Desktop.
Save you21979/5983025 to your computer and use it in GitHub Desktop.
node.jsでファイルを一行づつ読み込む
/**
* 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);
});
};
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();
@you21979
Copy link
Author

数ギガのテキストファイルを一行づつ処理する必要があったので即席で作った

@you21979
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment