30秒学会 JavaScript 片段 · 2018年6月22日

30秒学会 JavaScript 片段 – readFileLines

Returns an array of lines from the specified file.

Use readFileSync function in fs node package to create a Buffer from a file.
convert buffer to string using toString(encoding) function.
creating an array from contents of file by spliting file content line by line (each \n).

代码片段

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']