Menu

gitpiper

readFileLines javascript Code Snippet in 2023

nodearraybeginner

Last Updated: 3 February 2023

Returns an array of lines from the specified file.

  • Use fs.readFileSync() to create a Buffer from a file.
  • Convert buffer to string using buf.toString(encoding) function.
  • Use String.prototype.split(\n) 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 snippet similar to readFileLines For You in February 2023

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2023 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️