buffered-file-line-reader-sync

A simple tool that let you synchronously iterate through the lines of a file without loading the whole file in memory.

Usage no npm install needed!

<script type="module">
  import bufferedFileLineReaderSync from 'https://cdn.skypack.dev/buffered-file-line-reader-sync';
</script>

README

Synchroneous, Buffered, Line by Line File Reader

Simple library to read a file line by line, without loading the full file in memory. Ideal to read massive files.

Usage:

var BufferedFileLineReaderSync = require('buffered-file-line-reader-sync');

var line;
var filename = 'path/to/big/file';

var options = {
    encoding: 'utf8',
    bufferSize: 8192
}

bufferedReader = new BufferedFileLineReaderSync(filename, options);

while (bufferedReader.hasNextLine()) {
    console.log(bufferedReader.nextLine());
}

API:

Class: BufferedFileLineReaderSync(path [, options])

  • path: path to the file to read
  • options: object with the following parameters:
    • string encoding (defaults to 'utf8'): any encoding supported by Buffer.toString method of the Buffer node base class.
    • int bufferSize (defaults to 8192): size in octets (bytes) of the underlying buffer used.

nextLine()

Reads the next line in the file. Returns null if done reading.

hasNextLine()

Returns a boolean value to indicate if all lines have been read.