Returns an array of lines from the specified file.
fs.readFileSync()
to create a Buffer
from a file.Buffer.prototype.toString()
to covert the buffer to a string.String.prototype.split()
to create an array of lines from the contents of the file.const fs = require('fs');
const readFileLines = filename =>
fs
.readFileSync(filename)
.toString('UTF8')
.split('\n');
/*
contents of test.txt :
line1
line2
line3
___________________________
*/
let arr = readFileLines('test.txt');
console.log(arr); // ['line1', 'line2', 'line3']
JavaScript, Node
Checks if the given argument is a duplex (readable and writable) stream.
JavaScript, Node
Checks if the given argument is a readable stream.
JavaScript, Node
Writes a JSON object to a file.