Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 241
hippietrail Avatar asked Dec 18 '12 12:12

hippietrail


People also ask

How do I read big files in node JS?

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.

How do I read a large csv file in Node?

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.

Which method is used to to read a file in node JS?

The fs.readFile() method is used to read files on your computer.


1 Answers

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.

like image 133
loganfsmyth Avatar answered Nov 15 '22 05:11

loganfsmyth