Bun: Process File Line by Line

In NodeJs I can use fs and readline to make an interface and read the file line by line this is usefull for large text files (for me 100GB +) how can I achieve similar thing using bun

I've tried to read the file like this:

const input = Bun.file("input.txt");

but it's the same as fs.readFile which is not what I want in this case.


Nodejs example

const readline = require("readline");
const fs = require("fs");


const input = fs.createReadStream("file.txt", {
  encoding: "utf16le",
});
const rl = readline.createInterface({ input });

rl.on("line", (line) => {
  // proccess line
})
1

1 Answer

As @GrafiCode mentioned in the comments

const foo = Bun.file("foo.txt");

does not read the file from the disk it creates BunFile which you can read in many ways one of them is stream

await foo.stream(); // contents as ReadableStream

you can read more here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Elena Rostova

Elena Rostova

Lead Health, Wellness & Medical Journalist

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.

Share this article
Twitter Facebook Pinterest