How do I do random access reads from (large) files using node.js?

hippietrail picture hippietrail · Dec 18, 2012 · Viewed 7.4k times · Source

Am I missing something or does node.js's standard file I/O module lack analogs of the usual file random access methods?

  • seek() / fseek()
  • tell() / ftell()

How does one read random fixed-size records from large files in node without these?

Answer

loganfsmyth picture loganfsmyth · Dec 24, 2012

tell is not, but it is pretty rare to not already know the position you are at in a file, or to not have a way to keep track yourself.

seek is exposed indirectly via the position argument of fs.read and fs.write. When given, the argument will seek to that location before performing its operation, and if null, it will use whatever previous position it had.