파일이 큰 경우 가장 쉬운 방법은 fs.Readstream
입니다.
var fs = require('fs');
var
remaining = "";
lineFeed = "\n",
lineNr = 0;
fs.createReadStream('data.txt', { encoding: 'utf-8' })
.on('data', function (chunk) {
// store the actual chunk into the remaining
remaining = remaining.concat(chunk);
// look that we have a linefeed
var lastLineFeed = remaining.lastIndexOf(lineFeed);
// if we don't have any we can continue the reading
if (lastLineFeed === -1) return;
var
current = remaining.substring(0, lastLineFeed),
lines = current.split(lineFeed);
// store from the last linefeed or empty it out
remaining = (lastLineFeed > remaining.length)
? remaining.substring(lastLineFeed + 1, remaining.length)
: "";
for (var i = 0, length = lines.length; i < length; i++) {
// process the actual line
_processLine(lines[i], lineNr++);
}
})
.on('end', function (close) {
// TODO I'm not sure this is needed, it depends on your data
// process the reamining data if needed
if (remaining.length > 0) _processLine(remaining, lineNr);
});
function _processLine(line, lineNumber) {
// UPDATE2 with parseFloat
var numbers = line.split(" ").map(function (item) { return parseFloat(item); });
console.log(numbers, lineNumber);
}
전체 파일을 읽고 줄을 만들어야합니다. – Raynos
파일이 너무 큰 경우 더 좋은 해결책이 있습니다. –
스트림을 생성하고 게으른에 넘기 : 이 http://stackoverflow.com/questions/6156501/read-a-file-one-line-at-a-time-in-node-js –