Read a file one line at a time in node.js?
Since Node.js v0.12 and as of Node.js v4.0.0, there is a stable readline core module. Here's the easiest way to read lines from a file, without any external modules:
const fs = require('fs');
const readline = require('readline');
async function processLineByLine() {
const fileStream = fs.createReadStream('input.txt');
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity
});
// Note: we use the crlfDelay option to recognize all instances of CR LF
// ('\r\n') in input.txt as a single line break.
for await (const line of rl) {
// Each line in input.txt will be successively available here as `line`.
console.log(`Line from file: ${line}`);
}
}
processLineByLine();
Or alternatively:
var lineReader = require('readline').createInterface({
input: require('fs').createReadStream('file.in')
});
lineReader.on('line', function (line) {
console.log('Line from file:', line);
});
The last line is read correctly (as of Node v0.12 or later), even if there is no final \n
.
UPDATE: this example has been added to Node's API official documentation.
node.js: read a text file into an array. (Each line an item in the array.)
If you can fit the final data into an array then wouldn't you also be able to fit it in a string and split it, as has been suggested?
In any case if you would like to process the file one line at a time you can also try something like this:
var fs = require('fs');
function readLines(input, func) {
var remaining = '';
input.on('data', function(data) {
remaining += data;
var index = remaining.indexOf('\n');
while (index > -1) {
var line = remaining.substring(0, index);
remaining = remaining.substring(index + 1);
func(line);
index = remaining.indexOf('\n');
}
});
input.on('end', function() {
if (remaining.length > 0) {
func(remaining);
}
});
}
function func(data) {
console.log('Line: ' + data);
}
var input = fs.createReadStream('lines.txt');
readLines(input, func);
EDIT: (in response to comment by phopkins) I think (at least in newer versions) substring does not copy data but creates a special SlicedString object (from a quick glance at the v8 source code). In any case here is a modification that avoids the mentioned substring (tested on a file several megabytes worth of "All work and no play makes Jack a dull boy"):
function readLines(input, func) {
var remaining = '';
input.on('data', function(data) {
remaining += data;
var index = remaining.indexOf('\n');
var last = 0;
while (index > -1) {
var line = remaining.substring(last, index);
last = index + 1;
func(line);
index = remaining.indexOf('\n', last);
}
remaining = remaining.substring(last);
});
input.on('end', function() {
if (remaining.length > 0) {
func(remaining);
}
});
}
How to read a file line by line in Javascript and store it in an array
Maybe this will help you.
Async Version:
const fs = require('fs')
const emails = [];
const names = [];
fs.readFile('file.txt', (err, file) => {
if (err) throw err;
file.toString().split('\n').forEach(line => {
const splitedLine = line.split(':');
emails.push(splitedLine[0]);
names.push(splitedLine[1]);
});
});
Sync Version:
const fs = require('fs')
const emails = [];
const names = [];
fs.readFileSync('file.txt').toString().split('\n').forEach(line => {
const splitedLine = line.split(':');
emails.push(splitedLine[0]);
names.push(splitedLine[1]);
})
console.log(emails)
console.log(names)
How to read a file line by line 1 line at a time
I dont really understand why you use readline interface to read a file.
I suggest you to use split, such as
var splitstream = fs.createReadStream(file).pipe(split())
splitstream
.on('data', function (line) {
//each chunk now is a seperate line!
splitstream.pause();
setTimeout(function (){ splitstream.resume() }, 500);
})
you will definitely get one line at a time.
** edited for the comment.
Related Topics
How to Detect Internet Speed in JavaScript
Why Does Babel Rewrite Imported Function Call to (0, Fn)(...)
How to Find Object in Array by Property in JavaScript
Clientheight/Clientwidth Returning Different Values on Different Browsers
How to Inject CSS into Webpage Through Chrome Extension
"Force Reflow" in CSS Transitions in Bootstrap
How to Have a Host and Container Read/Write the Same Files with Docker
Adding a Parameter to the Url with JavaScript
How to Delete an Item from State Array
Display Data Streamed from a Flask View as It Updates
Why Is Null an Object and What's the Difference Between Null and Undefined
Am I Using Too Much Jquery? When am I Crossing the Line
How to Concatenate and Minify Multiple CSS and JavaScript Files with Grunt.Js (0.3.X)
Change CSS Properties on Click
CSS Transitions on New Elements
How to Build & Deploy a Samsung Smarttv App Without the Ide (E.G: on Linux)