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?
The most straightforward is fs. readFile() wherein, the whole file is read into memory and then acted upon once Node has read it, and the second option is fs. createReadStream() , which streams the data in (and out) similar to other languages like Python and Java.
You will use the fs module's createReadStream() method to read the data from the CSV file and create a readable stream. You will then pipe the stream to another stream initialized with the csv-parse module to parse the chunks of data. Once the chunks of data have been parsed, you can log them in the console.
The fs.readFile() method is used to read files on your computer.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With