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...
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 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